emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Jarmo Hurri <jarmo.hurri@iki.fi>
To: emacs-orgmode@gnu.org
Subject: Babel: command line args in java
Date: Tue, 05 Nov 2019 17:21:51 +0200	[thread overview]
Message-ID: <87imnypcvk.fsf@iki.fi> (raw)


* 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	other threads:[~2019-11-05 15:22 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-11-05 15:21 Jarmo Hurri [this message]
2022-10-20  3:02 ` Babel: command line args in java Ihor Radchenko

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87imnypcvk.fsf@iki.fi \
    --to=jarmo.hurri@iki.fi \
    --cc=emacs-orgmode@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public 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).