emacs-orgmode
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Babel: command line args in java


From: Jarmo Hurri
Subject: Babel: command line args in java
Date: Tue, 05 Nov 2019 17:21:51 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/26.2 (gnu/linux)

* 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~ ?




reply via email to

[Prev in Thread] Current Thread [Next in Thread]