emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* Babel: command line args in java
@ 2019-11-05 15:21 Jarmo Hurri
  0 siblings, 0 replies; only message in thread
From: Jarmo Hurri @ 2019-11-05 15:21 UTC (permalink / raw)
  To: emacs-orgmode


* Intro
  I am trying to supply command line arguments to java code in
  babel. For this a logical option would be to use ~:cmdline~ header
  argument, but I can not get it to work.

  I really need to process the given values as command line arguments,
  so setting some variable values will not work.

* This does not work
  Because ~ob-java.el~ concatenates ~:cmdline~ before ~:classname~ ,
  the straightforward way fails.

  #+begin_src java :classname ArgumentExample :cmdline hi there org fans :results output verbatim 
    class ArgumentExample
    {
      public static void main (String[] args)
      {
        for (String s : args)
          System.out.println (s);
      }
    }
  #+end_src

  #+RESULTS:

  The results is
  #+begin_center
  Error: Could not find or load main class hi
  Caused by: java.lang.ClassNotFoundException: hi
  #+end_center

  The reason is this piece of code in ~ob-java.el~
  #+begin_src elisp
    (org-babel-eval (concat org-babel-java-command " " cmdline " " classname) "")
  #+end_src

* This compiles and runs but not as desired
  The second try is to provide classname as the first argument. But a
  ~:classname~ header is also required, so the end result is not the
  desired one.

  #+begin_src java :classname ArgumentExample :cmdline ArgumentExample hi there org fans :results output verbatim 
    class ArgumentExample
    {
      public static void main (String[] args)
      {
        for (String s : args)
          System.out.println (s);
      }
    }
  #+end_src

  #+RESULTS:
  : hi
  : there
  : org
  : fans
  : ArgumentExample

* This fix does not work either
  When I try to leave out ~:classname~ , the process fails in early stages.

  #+begin_src java :cmdline ArgumentExample hi there org fans :results output verbatim 
    class ArgumentExample
    {
      public static void main (String[] args)
      {
        for (String s : args)
          System.out.println (s);
      }
    }
  #+end_src

  #+begin_center
  org-babel-execute:java: Can’t compile a java block without a classname
  #+end_center
* Questions
  1. Is there a correct way of doing this already?
  2. If there is no better way, is this desired behaviour or should it
     be fixed?
  3. If this is desired behaviour, can I write a patch adding a new
     header argument such as ~cmdargs~ ?

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2019-11-05 15:22 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-11-05 15:21 Babel: command line args in java Jarmo Hurri

Code repositories for project(s) associated with this inbox:

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).