emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* How-to evaluate Java-snippets in org-mode/org-babel?
@ 2011-07-05  7:01 Torsten Wagner
  2011-07-05  9:07 ` Eric S Fraga
  0 siblings, 1 reply; 17+ messages in thread
From: Torsten Wagner @ 2011-07-05  7:01 UTC (permalink / raw)
  To: Org Mode Mailing List

Hi all,

I need to evaluate many small java snippets. I tried to do this in 
org-mode but so fare it didn't work out. org-babel seems to have no java 
support ?

I can put the snippets in #BEGIN_SRC #END_SRC brackets which works nice 
for archiving and reporting. I can even call them in there own buffer 
via C-'. Now I would like to execute them to see if they work. Most of 
them just create some console output.
If I could simply execute the buffer I'm getting via C-' and see the 
output this would be totally sufficient.

I have trouble with different aspects of this. The buffer called by C-' 
has an unfortunate naming e.g. *Org Src main.org[ java ]*.... simply 
saving this buffer and trying to compile it troubles the java compiler.
Giving it a different name works, but then I face the problem that I 
need the following directories and name convention:

directory: <package_name>\<class_name>.java
to call
javac <package_name>\<class_name>.java
to create the class file and
java <package>name.<classname>
to execute the file.

Obviously, I would prefer to to this automatically from within emacs.

Ideally I would love to use org-babel. If this doesn't work out. A 
command to create the directories and filenames on fly, compile and 
execute it would be awesome too.

If there is someone who did something similar or has an idea... would be 
glad to hear about


Totti

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: How-to evaluate Java-snippets in org-mode/org-babel?
  2011-07-05  7:01 How-to evaluate Java-snippets in org-mode/org-babel? Torsten Wagner
@ 2011-07-05  9:07 ` Eric S Fraga
  2011-07-05  9:22   ` Eric S Fraga
                     ` (2 more replies)
  0 siblings, 3 replies; 17+ messages in thread
From: Eric S Fraga @ 2011-07-05  9:07 UTC (permalink / raw)
  To: Torsten Wagner; +Cc: Org Mode Mailing List

Torsten Wagner <torsten.wagner@gmail.com> writes:

> Hi all,
>
> I need to evaluate many small java snippets. I tried to do this in
> org-mode but so fare it didn't work out. org-babel seems to have no
> java support ?

Not directly but, given the issues with class paths and naming schemes,
I would suggest that the best route is to use babel to tangle code
instead of trying to execute it directly within org.  So, for instance,
you might have:

--8<---------------cut here---------------start------------->8---
#+begin_src java :tangle "mypackage/myclass.java"
package mypackage;
public class myclass
{
   ...
}
#+end_src
--8<---------------cut here---------------end--------------->8---

Then "C-c v t" in org will put the code into the appropriate file.  

You can have different snippets of code (e.g. each individual method in
the class) as separate source code blocks which can be tangled to the
same file.  You can also have different destinations for tangling in the
case that you have different classes.

Finally, you could then have a shell script, within org, to execute the
particular Java class you want:

--8<---------------cut here---------------start------------->8---
#+begin_src sh :results output
javac -cp . mypackage/myclass.java
java -cp . mypackage.myclass
#+end_src
--8<---------------cut here---------------end--------------->8---

Untested but I do this (on Linux, not sure about Windows however) with
Octave all the time, despite Octave being supported by org directly.
When I have multiple functions, this approach is easier.

HTH.
-- 
: Eric S Fraga (GnuPG: 0xC89193D8FFFCF67D) in Emacs 24.0.50.1
: using Org-mode version 7.5 (release_7.5.574.g5a503)

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: How-to evaluate Java-snippets in org-mode/org-babel?
  2011-07-05  9:07 ` Eric S Fraga
@ 2011-07-05  9:22   ` Eric S Fraga
  2011-07-05 13:48   ` Jason F. McBrayer
  2011-07-06 12:28   ` Torsten Wagner
  2 siblings, 0 replies; 17+ messages in thread
From: Eric S Fraga @ 2011-07-05  9:22 UTC (permalink / raw)
  To: Torsten Wagner; +Cc: Org Mode Mailing List

Eric S Fraga <e.fraga@ucl.ac.uk> writes:

> Torsten Wagner <torsten.wagner@gmail.com> writes:
>
>> Hi all,
>>
>> I need to evaluate many small java snippets. I tried to do this in
>> org-mode but so fare it didn't work out. org-babel seems to have no
>> java support ?
>
> Not directly but, given the issues with class paths and naming schemes,
> I would suggest that the best route is to use babel to tangle code
> instead of trying to execute it directly within org.  So, for instance,
> you might have:
>
> #+begin_src java :tangle "mypackage/myclass.java"
> package mypackage;
> public class myclass
> {
>    ...
> }
> #+end_src
>
> Then "C-c v t" in org will put the code into the appropriate file.  

That should have been "C-c C-v t" (org-babel-tangle); apologies for
typographical error!

-- 
: Eric S Fraga (GnuPG: 0xC89193D8FFFCF67D) in Emacs 24.0.50.1
: using Org-mode version 7.5 (release_7.5.574.g5a503)

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: How-to evaluate Java-snippets in org-mode/org-babel?
  2011-07-05  9:07 ` Eric S Fraga
  2011-07-05  9:22   ` Eric S Fraga
@ 2011-07-05 13:48   ` Jason F. McBrayer
  2011-07-06 12:36     ` Torsten Wagner
  2011-07-06 13:26     ` Eric Schulte
  2011-07-06 12:28   ` Torsten Wagner
  2 siblings, 2 replies; 17+ messages in thread
From: Jason F. McBrayer @ 2011-07-05 13:48 UTC (permalink / raw)
  To: Org Mode Mailing List

On Tue, 05 Jul 2011 10:07:19 +0100, Eric S Fraga wrote:
> Torsten Wagner writes:
>
>> Hi all, I need to evaluate many small java snippets. I tried to do 
>> this
>> in org-mode but so fare it didn't work out. org-babel seems to have 
>> no
>> java support ?
>
> Not directly but, given the issues with class paths and naming 
> schemes,
> I would suggest that the best route is to use babel to tangle code
> instead of trying to execute it directly within org.

My guess is that if you want to execute it directly within org, the
thing to do is to run it in beanshell[1], like JDEE does. I'm not
really familiar enough with org-babel to tell you how to go about
doing that, though.


[1] http://www.beanshell.org/

-- 
+-----------------------------------------------------------+
| Jason F. McBrayer                    jmcbray@carcosa.net  |
| If someone conquers a thousand times a thousand others in |
| battle, and someone else conquers himself, the latter one |
| is the greatest of all conquerors.  --- The Dhammapada    |

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: How-to evaluate Java-snippets in org-mode/org-babel?
  2011-07-05  9:07 ` Eric S Fraga
  2011-07-05  9:22   ` Eric S Fraga
  2011-07-05 13:48   ` Jason F. McBrayer
@ 2011-07-06 12:28   ` Torsten Wagner
  2011-07-06 13:38     ` Eric Schulte
  2 siblings, 1 reply; 17+ messages in thread
From: Torsten Wagner @ 2011-07-06 12:28 UTC (permalink / raw)
  To: Org Mode Mailing List

Hi Eric,

Somehow I was pretty sure you would answer ;)

I tried your example and it work great so far.

I have three points which I would like to discuss

1. I thought it might be nice to declare the package and class name for 
the java-stuff as variable in a property node then I could do something 
like:

* Coursework 1
:PROPERTIES:
:var: PKGNAME="cw1"
:var: CLASS="calc"
:END:

** Snippet 1
** Snippet 2

* Coursework 2
:PROPERTIES:
:var: PKGNAME="cw2"
:var: CLASS="string"
:END:

** Snippet 1
** Snippet 2

Unfortunately, org-mode does not allow multiple definition of the same 
property. I know there is a work around with a table. However, I thought 
I could use simply a list.

:var: vars="(cw1 calc)"
and use this in my shell script. However, this doesn't work. I can't 
index them. Any idea? Should it work in principle? Could I e.g., use
:var: vars="{'x': 128, 'y': 210}"
and put this in a python block accessing them by vars['x'] and 
vars['y']? That would be great!


2. I'm not totally sure but it seems that tangling creates the desired 
file if it is not existing so fare but it does not create folders thus,
:tangle path/filename seems not to work.
Any reason for this or maybe good idea to add it as a feature?
For now, I added a little shell-based block "start" which takes care of 
this.

3. Tangleing and executing the code via a shell blocks works great. I 
saw somewhere that you could even tangle from the command line by 
calling emacs in batch mode. That's maybe a bit overcomplex for my task 
but some additional style to noweb might be nice. Instead of placing the 
code block at the point of call, tangle the code block. Then I could call

<<start>>
<<snippet>>
<<execute>>

If I understood right, noweb for now only place the code of the block at 
the particular place. Maybe, I could write a lisp-block "tangle" which 
tangles the code and call it via
<<tangle(snippet)>>
Just want to know if this might be a good idea in general or if this 
works already in some other way.

All the best

Totti

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: How-to evaluate Java-snippets in org-mode/org-babel?
  2011-07-05 13:48   ` Jason F. McBrayer
@ 2011-07-06 12:36     ` Torsten Wagner
  2011-07-06 13:26     ` Eric Schulte
  1 sibling, 0 replies; 17+ messages in thread
From: Torsten Wagner @ 2011-07-06 12:36 UTC (permalink / raw)
  To: Jason F. McBrayer; +Cc: Org Mode Mailing List

Hi Jason,

> My guess is that if you want to execute it directly within org, the
> thing to do is to run it in beanshell[1], like JDEE does. I'm not
> really familiar enough with org-babel to tell you how to go about
> doing that, though.

Maybe this works nice too. However, so fare Erics solution turns out 
nice. Would need to see which advantages I would have using beanshell. 
Maybe code debugging might be easier then.

Thanks for the tipp

Totti

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: How-to evaluate Java-snippets in org-mode/org-babel?
  2011-07-05 13:48   ` Jason F. McBrayer
  2011-07-06 12:36     ` Torsten Wagner
@ 2011-07-06 13:26     ` Eric Schulte
  2011-07-06 15:14       ` Jason F. McBrayer
  1 sibling, 1 reply; 17+ messages in thread
From: Eric Schulte @ 2011-07-06 13:26 UTC (permalink / raw)
  To: Jason F. McBrayer; +Cc: Org Mode Mailing List

"Jason F. McBrayer" <jmcbray@carcosa.net> writes:

> On Tue, 05 Jul 2011 10:07:19 +0100, Eric S Fraga wrote:
>> Torsten Wagner writes:
>>
>>> Hi all, I need to evaluate many small java snippets. I tried to do
>>> this
>>> in org-mode but so fare it didn't work out. org-babel seems to have
>>> no
>>> java support ?
>>
>> Not directly but, given the issues with class paths and naming
>> schemes,
>> I would suggest that the best route is to use babel to tangle code
>> instead of trying to execute it directly within org.
>
> My guess is that if you want to execute it directly within org, the
> thing to do is to run it in beanshell[1], like JDEE does. I'm not
> really familiar enough with org-babel to tell you how to go about
> doing that, though.
>
>
> [1] http://www.beanshell.org/

I think the first place to look for executing Java from Org-mode code
blocks would be to check if any existing Emacs java modes already
provide support for interactive code evaluation.  When possible
piggybacking off of existing support modes is generally the easiest
solution.

See org/ob-C.el for an example of how to execute a code block which
requires compilation before execution.  I've (thankfully) managed to
avoid using Java throughout my CS career, so I'm not overly familiar
with the execution mechanics.  There are numerous examples in the ob-*
files of languages which write code block bodies to external files and
then pass those files to executables.

beanshell looks like it may provide the best option for :session
evaluation, however if no inferior java mode currently exists I would be
hesitant to try to wrap beanshell directly.

Cheers -- Eric

-- 
Eric Schulte
http://cs.unm.edu/~eschulte/

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: How-to evaluate Java-snippets in org-mode/org-babel?
  2011-07-06 12:28   ` Torsten Wagner
@ 2011-07-06 13:38     ` Eric Schulte
  2011-07-14  5:42       ` Torsten Wagner
  0 siblings, 1 reply; 17+ messages in thread
From: Eric Schulte @ 2011-07-06 13:38 UTC (permalink / raw)
  To: Torsten Wagner; +Cc: Org Mode Mailing List

Torsten Wagner <torsten.wagner@gmail.com> writes:

> Hi Eric,
>
> Somehow I was pretty sure you would answer ;)
>
> I tried your example and it work great so far.
>
> I have three points which I would like to discuss
>
> 1. I thought it might be nice to declare the package and class name
> for the java-stuff as variable in a property node then I could do
> something like:
>
> * Coursework 1
> :PROPERTIES:
> :var: PKGNAME="cw1"
> :var: CLASS="calc"
> :END:
>
> ** Snippet 1
> ** Snippet 2
>
> * Coursework 2
> :PROPERTIES:
> :var: PKGNAME="cw2"
> :var: CLASS="string"
> :END:
>
> ** Snippet 1
> ** Snippet 2
>
> Unfortunately, org-mode does not allow multiple definition of the same
> property. I know there is a work around with a table. However, I
> thought I could use simply a list.
>
> :var: vars="(cw1 calc)"

quotes force string interpretation, try something like the following...

#+begin_src emacs-lisp :var lst='(0 1 2)
  (first lst)
#+end_src

#+results:
: 0

or as a noweb reference

#+data: external-list
- 0
- 1
- 2

#+begin_src sh :noweb yes
  echo <<external-list()[0]>>
#+end_src

#+results:
: 0

> 
> and use this in my shell script. However, this doesn't work. I can't
> index them. Any idea? Should it work in principle? Could I e.g., use
> :var: vars="{'x': 128, 'y': 210}" and put this in a python block
> accessing them by vars['x'] and vars['y']? That would be great!
>

The only data types which can be passed between Org-mode code blocks
currently are lists (of arbitrary dimension), numbers and strings.
Hashes (or "dictionaries") are not currently supported.

>
>
> 2. I'm not totally sure but it seems that tangling creates the desired
> file if it is not existing so fare but it does not create folders
> thus, :tangle path/filename seems not to work.

see the :mkdirp header argument http://orgmode.org/manual/mkdirp.html

> 
> Any reason for this or maybe good idea to add it as a feature?  For
> now, I added a little shell-based block "start" which takes care of
> this.
>
> 3. Tangleing and executing the code via a shell blocks works great. I
> saw somewhere that you could even tangle from the command line by
> calling emacs in batch mode. That's maybe a bit overcomplex for my
> task but some additional style to noweb might be nice. Instead of
> placing the code block at the point of call, tangle the code
> block. Then I could call
>
> <<start>>
> <<snippet>>
> <<execute>>
>
> If I understood right, noweb for now only place the code of the block
> at the particular place. Maybe, I could write a lisp-block "tangle"
> which tangles the code and call it via
> <<tangle(snippet)>>
> Just want to know if this might be a good idea in general or if this
> works already in some other way.
>

You probably don't want to pass the body of a code block to a lisp
function as quoting will become a nightmare, however passing the name to
a lisp block may be reasonable.

I would suggest that rather than implementing this behavior in a code
block you take a look at starting a ob-java.el file.  A partial
implementation (e.g., only external evaluation, no session evaluation)
would be a useful contribution, and given the many helper functions and
examples present in the other ob-* files this would most likely be
easier than a custom lisp-block implementation.

Best -- Eric

>
> All the best
>
> Totti
>

-- 
Eric Schulte
http://cs.unm.edu/~eschulte/

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: How-to evaluate Java-snippets in org-mode/org-babel?
  2011-07-06 13:26     ` Eric Schulte
@ 2011-07-06 15:14       ` Jason F. McBrayer
  2011-07-06 17:11         ` Torsten Wagner
  2011-07-06 19:15         ` Eric S Fraga
  0 siblings, 2 replies; 17+ messages in thread
From: Jason F. McBrayer @ 2011-07-06 15:14 UTC (permalink / raw)
  To: Eric Schulte; +Cc: Org Mode Mailing List

On Wed, 06 Jul 2011 07:26:45 -0600, Eric Schulte wrote:

> I think the first place to look for executing Java from Org-mode code
> blocks would be to check if any existing Emacs java modes already
> provide support for interactive code evaluation.

I believe JDEE does support this:

> jde-bsh-run is an interactive compiled Lisp function in `jde-bsh.el'.
>
> (jde-bsh-run)
>
> *Starts the JDEE version of the BeanShell.

But honestly, getting everything set up for your environment for it to 
actually work right is a pain and three quarters.

-- 
+-----------------------------------------------------------+
| Jason F. McBrayer jmcbray@carcosa.net                     |
| If someone conquers a thousand times a thousand others in |
| battle, and someone else conquers himself, the latter one |
| is the greatest of all conquerors. --- The Dhammapada     |

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: How-to evaluate Java-snippets in org-mode/org-babel?
  2011-07-06 15:14       ` Jason F. McBrayer
@ 2011-07-06 17:11         ` Torsten Wagner
  2011-07-06 19:15         ` Eric S Fraga
  1 sibling, 0 replies; 17+ messages in thread
From: Torsten Wagner @ 2011-07-06 17:11 UTC (permalink / raw)
  To: Jason F. McBrayer; +Cc: Org Mode Mailing List

> But honestly, getting everything set up for your environment for it to
> actually work right is a pain and three quarters.

+1
I tried to use JDEE. It was crazy complex to set-up and at some point I gave up.
It seems Java support under emacs got a bit weak.

@Eric..
well I never used Java much. Learned a bit about it 10 years ago. Now
they asked me to assist teaching a Java class..... you never now
what's coming next, right ;)

Totti

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: How-to evaluate Java-snippets in org-mode/org-babel?
  2011-07-06 15:14       ` Jason F. McBrayer
  2011-07-06 17:11         ` Torsten Wagner
@ 2011-07-06 19:15         ` Eric S Fraga
  1 sibling, 0 replies; 17+ messages in thread
From: Eric S Fraga @ 2011-07-06 19:15 UTC (permalink / raw)
  To: Jason F. McBrayer; +Cc: Org Mode Mailing List

"Jason F. McBrayer" <jmcbray@carcosa.net> writes:

> On Wed, 06 Jul 2011 07:26:45 -0600, Eric Schulte wrote:
>
>> I think the first place to look for executing Java from Org-mode code
>> blocks would be to check if any existing Emacs java modes already
>> provide support for interactive code evaluation.
>
> I believe JDEE does support this:
>
>> jde-bsh-run is an interactive compiled Lisp function in `jde-bsh.el'.
>>
>> (jde-bsh-run)
>>
>> *Starts the JDEE version of the BeanShell.
>
> But honestly, getting everything set up for your environment for it to
> actually work right is a pain and three quarters.

How true; the only reason I have emacs 23 still installed on my system
is the continuing failure to install jdee, ecb, et al. on Emacs 24.
Trying to get something (compatible across emacs versions) running to
support Java within org is likely to be a nightmare.  However, I hope
somebody on this list can prove me wrong!

-- 
: Eric S Fraga (GnuPG: 0xC89193D8FFFCF67D) in Emacs 24.0.50.1
: using Org-mode version 7.5 (release_7.5.586.g382e6)

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: How-to evaluate Java-snippets in org-mode/org-babel?
  2011-07-06 13:38     ` Eric Schulte
@ 2011-07-14  5:42       ` Torsten Wagner
  2011-07-15 15:00         ` Eric Schulte
  0 siblings, 1 reply; 17+ messages in thread
From: Torsten Wagner @ 2011-07-14  5:42 UTC (permalink / raw)
  To: Eric Schulte; +Cc: Org Mode Mailing List

Hi Eric,

> You probably don't want to pass the body of a code block to a lisp
> function as quoting will become a nightmare, however passing the name to
> a lisp block may be reasonable.
>
> I would suggest that rather than implementing this behavior in a code
> block you take a look at starting a ob-java.el file.  A partial
> implementation (e.g., only external evaluation, no session evaluation)
> would be a useful contribution, and given the many helper functions and
> examples present in the other ob-* files this would most likely be
> easier than a custom lisp-block implementation.

o.k. the first round of evaluations is over and it worked out o.k. 
However, there was still rather much handwork to do.
I tried to get a ob-java.el file together using the template and mainly 
by looking at ob-c.el which I guess comes close what would have to be 
done for java.
However, my lisp-skills (which are close to zero) are not enough to get 
it working. The main problem was that ob-c.el is working for both C and 
C++ and all this if then else troubles me a bit.

Basically, I want only tangle the actual code block into a temp file. 
Well temp is not really right, since java demand its file structure and 
file naming. Finally execute it externally by your proposed code

javac -cp . mypackage/myclass.java
java -cp . mypackage.myclass

and return the results

Hmm maybe better to give a real world example (stripped down to make it 
shorter)
I use now the following way
/-----------------------------------------------/
#+BABEL: :mkdirp t

* Coursework 1
** StudentID1
#+BEGIN_SRC java
   package foo;
   public class Bar
   {
           private double ans = 0
           public void set(double d){
                   ans = d;
           }
           public void print(){
                   System.out.println(ans);
           }
           public static void main(String[] argv){
                   Bar c = new Bar();
                   c.set(100);
                   c.print();
           }
   }
#+end_src

** StudentID2
#+BEGIN_SRC java
   package foo;
   public class Bar
   {
           private double x = 0
           public void set(double in){
                   x = in;
           }
           public void print(){
                   System.out.println(x);
           }
           public static void main(String[] argv){
                   Bar myclass = new Bar();
                   myclass.set(100);
                   myclass.print();
           }
   }
#+end_src

** Result
#+srcname: result
#+begin_src sh :results output
javac -cp . foo/Bar.java
java -cp . foo.Bar
#+end_src

/----------------------------------------------------/


For now I only added the tangle command to a single code block and 
created the file via C-c C-v t.

#+BEGIN_SRC java tangle: foo/Bar.java

Then I rushed down to a shell block  "result" which executed the the 
above commands. I checked the results and started to remove the tangle 
object from one block and added it to the next block. Kind of tiring if 
you have several dozen of blocks.
Guess you can see from the above example the trouble of having several 
dozen of them and then tangeling them one by one and execute the result 
block ;)

I tried to make it more easy by giving the shell block a name and call 
it under each java code block. This would save me the time going up and 
down in my file.

#+call: result()

However, I noticed that the result update was always done at the first 
appearances of the call , like under the first java code block but not 
at the desired code block?!
if you fold all together it would look like

/-----------------------------------------------/
#+BABEL: :mkdirp t

* Coursework 1
** StudentID1
#+BEGIN_SRC java
#+call: result()

#+results: result
: 100.0

** StudentID2
#+BEGIN_SRC java :tangle foo/Bar.java
#+call: result()

** Result
#+srcname: result
/-----------------------------------------------/

Calling the second call function updates the result on the first!

Anyhow, I guess having it working with a ob-java.el minimal system would 
be the most easiest. Simply type C-c C-c and it would be done.

Would be very glad if you could help me to get this somehow working.

Totti

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: How-to evaluate Java-snippets in org-mode/org-babel?
  2011-07-14  5:42       ` Torsten Wagner
@ 2011-07-15 15:00         ` Eric Schulte
  2011-07-18  1:31           ` Torsten Wagner
  0 siblings, 1 reply; 17+ messages in thread
From: Eric Schulte @ 2011-07-15 15:00 UTC (permalink / raw)
  To: Torsten Wagner; +Cc: Org Mode Mailing List

[-- Attachment #1: Type: text/plain, Size: 6373 bytes --]

Hi Torsten,

I've just written the included emacs-lisp function [1], which when added
to your config should provide for minimal evaluation functionality for
java code blocks.

This function drops the body of a code block to a file named after the
value of the :classname header argument (since java cares about file
names), it then compiles the file with javac, and executes the resulting
executable returning the results.  See the attached Org-mode file [2]
which evaluates successfully on my system (after I installed javac).

I copied the code example and compilation process from [3].

Please let me know how this works for you, hopefully once we iron out
the kinks in this function it can serve as the seed for a full java
mode.

Cheers -- Eric

Torsten Wagner <torsten.wagner@gmail.com> writes:

> Hi Eric,
>
>> You probably don't want to pass the body of a code block to a lisp
>> function as quoting will become a nightmare, however passing the name to
>> a lisp block may be reasonable.
>>
>> I would suggest that rather than implementing this behavior in a code
>> block you take a look at starting a ob-java.el file.  A partial
>> implementation (e.g., only external evaluation, no session evaluation)
>> would be a useful contribution, and given the many helper functions and
>> examples present in the other ob-* files this would most likely be
>> easier than a custom lisp-block implementation.
>
> o.k. the first round of evaluations is over and it worked out
> o.k. However, there was still rather much handwork to do.
> I tried to get a ob-java.el file together using the template and
> mainly by looking at ob-c.el which I guess comes close what would have
> to be done for java.
> However, my lisp-skills (which are close to zero) are not enough to
> get it working. The main problem was that ob-c.el is working for both
> C and C++ and all this if then else troubles me a bit.
>
> Basically, I want only tangle the actual code block into a temp
> file. Well temp is not really right, since java demand its file
> structure and file naming. Finally execute it externally by your
> proposed code
>
> javac -cp . mypackage/myclass.java
> java -cp . mypackage.myclass
>
> and return the results
>
> Hmm maybe better to give a real world example (stripped down to make
> it shorter)
> I use now the following way
> /-----------------------------------------------/
> #+BABEL: :mkdirp t
>
> * Coursework 1
> ** StudentID1
> #+BEGIN_SRC java
>   package foo;
>   public class Bar
>   {
>           private double ans = 0
>           public void set(double d){
>                   ans = d;
>           }
>           public void print(){
>                   System.out.println(ans);
>           }
>           public static void main(String[] argv){
>                   Bar c = new Bar();
>                   c.set(100);
>                   c.print();
>           }
>   }
> #+end_src
>
> ** StudentID2
> #+BEGIN_SRC java
>   package foo;
>   public class Bar
>   {
>           private double x = 0
>           public void set(double in){
>                   x = in;
>           }
>           public void print(){
>                   System.out.println(x);
>           }
>           public static void main(String[] argv){
>                   Bar myclass = new Bar();
>                   myclass.set(100);
>                   myclass.print();
>           }
>   }
> #+end_src
>
> ** Result
> #+srcname: result
> #+begin_src sh :results output
> javac -cp . foo/Bar.java
> java -cp . foo.Bar
> #+end_src
>
> /----------------------------------------------------/
>
>
> For now I only added the tangle command to a single code block and
> created the file via C-c C-v t.
>
> #+BEGIN_SRC java tangle: foo/Bar.java
>
> Then I rushed down to a shell block  "result" which executed the the
> above commands. I checked the results and started to remove the tangle
> object from one block and added it to the next block. Kind of tiring
> if you have several dozen of blocks.
> Guess you can see from the above example the trouble of having several
> dozen of them and then tangeling them one by one and execute the
> result block ;)
>
> I tried to make it more easy by giving the shell block a name and call
> it under each java code block. This would save me the time going up
> and down in my file.
>
> #+call: result()
>
> However, I noticed that the result update was always done at the first
> appearances of the call , like under the first java code block but not
> at the desired code block?!
> if you fold all together it would look like
>
> /-----------------------------------------------/
> #+BABEL: :mkdirp t
>
> * Coursework 1
> ** StudentID1
> #+BEGIN_SRC java
> #+call: result()
>
> #+results: result
> : 100.0
>
> ** StudentID2
> #+BEGIN_SRC java :tangle foo/Bar.java
> #+call: result()
>
> ** Result
> #+srcname: result
> /-----------------------------------------------/
>
> Calling the second call function updates the result on the first!
>
> Anyhow, I guess having it working with a ob-java.el minimal system
> would be the most easiest. Simply type C-c C-c and it would be done.
>
> Would be very glad if you could help me to get this somehow working.
>
> Totti
>
>


Footnotes: 
[1]  
#+begin_src emacs-lisp
  (defun org-babel-execute:java (body params)
    (let* ((classname (or (cdr (assoc :classname params))
                          (error
                           "Can't compile a java block without a classname")))
           (src-file (concat classname ".java"))
           (full-body (org-babel-expand-body:generic body params))
           (compile
            (progn (with-temp-file src-file (insert full-body))
                   (org-babel-eval (concat "javac " src-file) ""))))
      ((lambda (results)
         (org-babel-reassemble-table
          (if (member "vector" (cdr (assoc :result-params params)))
              (let ((tmp-file (org-babel-temp-file "c-")))
                (with-temp-file tmp-file (insert results))
                (org-babel-import-elisp-from-file tmp-file))
            (org-babel-read results))
          (org-babel-pick-name
           (cdr (assoc :colname-names params)) (cdr (assoc :colnames params)))
          (org-babel-pick-name
           (cdr (assoc :rowname-names params)) (cdr (assoc :rownames params)))))
       (org-babel-eval (concat "java " classname) ""))))
#+end_src

[2]  hello-java.org

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: hello-java.org --]
[-- Type: text/x-org, Size: 224 bytes --]


#+begin_src java :classname myfirstjavaprog
  class myfirstjavaprog
  {  
      public static void main(String args[])
      {
          System.out.println("Hello World!");
      }
  }
#+end_src

#+results:
: Hello World!


[-- Attachment #3: Type: text/plain, Size: 105 bytes --]


[3]  http://www.javacoffeebreak.com/java101/java101.html

-- 
Eric Schulte
http://cs.unm.edu/~eschulte/

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: How-to evaluate Java-snippets in org-mode/org-babel?
  2011-07-15 15:00         ` Eric Schulte
@ 2011-07-18  1:31           ` Torsten Wagner
  2011-07-18 14:53             ` Eric Schulte
  0 siblings, 1 reply; 17+ messages in thread
From: Torsten Wagner @ 2011-07-18  1:31 UTC (permalink / raw)
  To: Eric Schulte; +Cc: Org Mode Mailing List

[-- Attachment #1: Type: text/plain, Size: 6754 bytes --]

Sorry took me a while to test it.
It works great !!!!
Many many thanks for your help.
I found two minor things.
My snippets contain the definition of a package, which in turn end to be a
folder. The tangle function could create folders on demand. Would be useful
for your code too. It works already by creating the folder manually and
simply write...

#+begin_src java classname packagename/classname

in the next step, people might use sessions as equivalent to a package. This
would allow to define multiple classes and the usage of them within a single
execution.

However for now its perfect already. Why not putting it into ob-java.el and
see how its develope.
Totti
On Jul 16, 2011 12:37 AM, "Eric Schulte" <schulte.eric@gmail.com> wrote:
> Hi Torsten,
>
> I've just written the included emacs-lisp function [1], which when added
> to your config should provide for minimal evaluation functionality for
> java code blocks.
>
> This function drops the body of a code block to a file named after the
> value of the :classname header argument (since java cares about file
> names), it then compiles the file with javac, and executes the resulting
> executable returning the results. See the attached Org-mode file [2]
> which evaluates successfully on my system (after I installed javac).
>
> I copied the code example and compilation process from [3].
>
> Please let me know how this works for you, hopefully once we iron out
> the kinks in this function it can serve as the seed for a full java
> mode.
>
> Cheers -- Eric
>
> Torsten Wagner <torsten.wagner@gmail.com> writes:
>
>> Hi Eric,
>>
>>> You probably don't want to pass the body of a code block to a lisp
>>> function as quoting will become a nightmare, however passing the name to
>>> a lisp block may be reasonable.
>>>
>>> I would suggest that rather than implementing this behavior in a code
>>> block you take a look at starting a ob-java.el file. A partial
>>> implementation (e.g., only external evaluation, no session evaluation)
>>> would be a useful contribution, and given the many helper functions and
>>> examples present in the other ob-* files this would most likely be
>>> easier than a custom lisp-block implementation.
>>
>> o.k. the first round of evaluations is over and it worked out
>> o.k. However, there was still rather much handwork to do.
>> I tried to get a ob-java.el file together using the template and
>> mainly by looking at ob-c.el which I guess comes close what would have
>> to be done for java.
>> However, my lisp-skills (which are close to zero) are not enough to
>> get it working. The main problem was that ob-c.el is working for both
>> C and C++ and all this if then else troubles me a bit.
>>
>> Basically, I want only tangle the actual code block into a temp
>> file. Well temp is not really right, since java demand its file
>> structure and file naming. Finally execute it externally by your
>> proposed code
>>
>> javac -cp . mypackage/myclass.java
>> java -cp . mypackage.myclass
>>
>> and return the results
>>
>> Hmm maybe better to give a real world example (stripped down to make
>> it shorter)
>> I use now the following way
>> /-----------------------------------------------/
>> #+BABEL: :mkdirp t
>>
>> * Coursework 1
>> ** StudentID1
>> #+BEGIN_SRC java
>> package foo;
>> public class Bar
>> {
>> private double ans = 0
>> public void set(double d){
>> ans = d;
>> }
>> public void print(){
>> System.out.println(ans);
>> }
>> public static void main(String[] argv){
>> Bar c = new Bar();
>> c.set(100);
>> c.print();
>> }
>> }
>> #+end_src
>>
>> ** StudentID2
>> #+BEGIN_SRC java
>> package foo;
>> public class Bar
>> {
>> private double x = 0
>> public void set(double in){
>> x = in;
>> }
>> public void print(){
>> System.out.println(x);
>> }
>> public static void main(String[] argv){
>> Bar myclass = new Bar();
>> myclass.set(100);
>> myclass.print();
>> }
>> }
>> #+end_src
>>
>> ** Result
>> #+srcname: result
>> #+begin_src sh :results output
>> javac -cp . foo/Bar.java
>> java -cp . foo.Bar
>> #+end_src
>>
>> /----------------------------------------------------/
>>
>>
>> For now I only added the tangle command to a single code block and
>> created the file via C-c C-v t.
>>
>> #+BEGIN_SRC java tangle: foo/Bar.java
>>
>> Then I rushed down to a shell block "result" which executed the the
>> above commands. I checked the results and started to remove the tangle
>> object from one block and added it to the next block. Kind of tiring
>> if you have several dozen of blocks.
>> Guess you can see from the above example the trouble of having several
>> dozen of them and then tangeling them one by one and execute the
>> result block ;)
>>
>> I tried to make it more easy by giving the shell block a name and call
>> it under each java code block. This would save me the time going up
>> and down in my file.
>>
>> #+call: result()
>>
>> However, I noticed that the result update was always done at the first
>> appearances of the call , like under the first java code block but not
>> at the desired code block?!
>> if you fold all together it would look like
>>
>> /-----------------------------------------------/
>> #+BABEL: :mkdirp t
>>
>> * Coursework 1
>> ** StudentID1
>> #+BEGIN_SRC java
>> #+call: result()
>>
>> #+results: result
>> : 100.0
>>
>> ** StudentID2
>> #+BEGIN_SRC java :tangle foo/Bar.java
>> #+call: result()
>>
>> ** Result
>> #+srcname: result
>> /-----------------------------------------------/
>>
>> Calling the second call function updates the result on the first!
>>
>> Anyhow, I guess having it working with a ob-java.el minimal system
>> would be the most easiest. Simply type C-c C-c and it would be done.
>>
>> Would be very glad if you could help me to get this somehow working.
>>
>> Totti
>>
>>
>
>
> Footnotes:
> [1]
> #+begin_src emacs-lisp
> (defun org-babel-execute:java (body params)
> (let* ((classname (or (cdr (assoc :classname params))
> (error
> "Can't compile a java block without a classname")))
> (src-file (concat classname ".java"))
> (full-body (org-babel-expand-body:generic body params))
> (compile
> (progn (with-temp-file src-file (insert full-body))
> (org-babel-eval (concat "javac " src-file) ""))))
> ((lambda (results)
> (org-babel-reassemble-table
> (if (member "vector" (cdr (assoc :result-params params)))
> (let ((tmp-file (org-babel-temp-file "c-")))
> (with-temp-file tmp-file (insert results))
> (org-babel-import-elisp-from-file tmp-file))
> (org-babel-read results))
> (org-babel-pick-name
> (cdr (assoc :colname-names params)) (cdr (assoc :colnames params)))
> (org-babel-pick-name
> (cdr (assoc :rowname-names params)) (cdr (assoc :rownames params)))))
> (org-babel-eval (concat "java " classname) ""))))
> #+end_src
>
> [2] hello-java.org

[-- Attachment #2: Type: text/html, Size: 9406 bytes --]

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: How-to evaluate Java-snippets in org-mode/org-babel?
  2011-07-18  1:31           ` Torsten Wagner
@ 2011-07-18 14:53             ` Eric Schulte
  2011-07-28 10:49               ` Torsten Wagner
  0 siblings, 1 reply; 17+ messages in thread
From: Eric Schulte @ 2011-07-18 14:53 UTC (permalink / raw)
  To: Torsten Wagner; +Cc: Org Mode Mailing List

Torsten Wagner <torsten.wagner@gmail.com> writes:

> Sorry took me a while to test it.
> It works great !!!!
> Many many thanks for your help.
> I found two minor things.
> My snippets contain the definition of a package, which in turn end to be a
> folder. The tangle function could create folders on demand. Would be useful
> for your code too. It works already by creating the folder manually and
> simply write...
>
> #+begin_src java classname packagename/classname
>

OK, I've changed this code so that missing package directories will be
created automatically.

>
> in the next step, people might use sessions as equivalent to a package. This
> would allow to define multiple classes and the usage of them within a single
> execution.
>

From earlier discussion in this thread, it sounds as though java session
integration is difficult and cumbersome, so for now I would lean towards
simply not including session support.

>
> However for now its perfect already. Why not putting it into ob-java.el and
> see how its develope.

Sounds good, I have just added a small ob-java.el to the Org-mode core.

Thanks for the feedback -- Eric

> 
> Totti
> On Jul 16, 2011 12:37 AM, "Eric Schulte" <schulte.eric@gmail.com> wrote:
>> Hi Torsten,
>>
>> I've just written the included emacs-lisp function [1], which when added
>> to your config should provide for minimal evaluation functionality for
>> java code blocks.
>>
>> This function drops the body of a code block to a file named after the
>> value of the :classname header argument (since java cares about file
>> names), it then compiles the file with javac, and executes the resulting
>> executable returning the results. See the attached Org-mode file [2]
>> which evaluates successfully on my system (after I installed javac).
>>
>> I copied the code example and compilation process from [3].
>>
>> Please let me know how this works for you, hopefully once we iron out
>> the kinks in this function it can serve as the seed for a full java
>> mode.
>>
>> Cheers -- Eric
>>
>> Torsten Wagner <torsten.wagner@gmail.com> writes:
>>
>>> Hi Eric,
>>>
>>>> You probably don't want to pass the body of a code block to a lisp
>>>> function as quoting will become a nightmare, however passing the name to
>>>> a lisp block may be reasonable.
>>>>
>>>> I would suggest that rather than implementing this behavior in a code
>>>> block you take a look at starting a ob-java.el file. A partial
>>>> implementation (e.g., only external evaluation, no session evaluation)
>>>> would be a useful contribution, and given the many helper functions and
>>>> examples present in the other ob-* files this would most likely be
>>>> easier than a custom lisp-block implementation.
>>>
>>> o.k. the first round of evaluations is over and it worked out
>>> o.k. However, there was still rather much handwork to do.
>>> I tried to get a ob-java.el file together using the template and
>>> mainly by looking at ob-c.el which I guess comes close what would have
>>> to be done for java.
>>> However, my lisp-skills (which are close to zero) are not enough to
>>> get it working. The main problem was that ob-c.el is working for both
>>> C and C++ and all this if then else troubles me a bit.
>>>
>>> Basically, I want only tangle the actual code block into a temp
>>> file. Well temp is not really right, since java demand its file
>>> structure and file naming. Finally execute it externally by your
>>> proposed code
>>>
>>> javac -cp . mypackage/myclass.java
>>> java -cp . mypackage.myclass
>>>
>>> and return the results
>>>
>>> Hmm maybe better to give a real world example (stripped down to make
>>> it shorter)
>>> I use now the following way
>>> /-----------------------------------------------/
>>> #+BABEL: :mkdirp t
>>>
>>> * Coursework 1
>>> ** StudentID1
>>> #+BEGIN_SRC java
>>> package foo;
>>> public class Bar
>>> {
>>> private double ans = 0
>>> public void set(double d){
>>> ans = d;
>>> }
>>> public void print(){
>>> System.out.println(ans);
>>> }
>>> public static void main(String[] argv){
>>> Bar c = new Bar();
>>> c.set(100);
>>> c.print();
>>> }
>>> }
>>> #+end_src
>>>
>>> ** StudentID2
>>> #+BEGIN_SRC java
>>> package foo;
>>> public class Bar
>>> {
>>> private double x = 0
>>> public void set(double in){
>>> x = in;
>>> }
>>> public void print(){
>>> System.out.println(x);
>>> }
>>> public static void main(String[] argv){
>>> Bar myclass = new Bar();
>>> myclass.set(100);
>>> myclass.print();
>>> }
>>> }
>>> #+end_src
>>>
>>> ** Result
>>> #+srcname: result
>>> #+begin_src sh :results output
>>> javac -cp . foo/Bar.java
>>> java -cp . foo.Bar
>>> #+end_src
>>>
>>> /----------------------------------------------------/
>>>
>>>
>>> For now I only added the tangle command to a single code block and
>>> created the file via C-c C-v t.
>>>
>>> #+BEGIN_SRC java tangle: foo/Bar.java
>>>
>>> Then I rushed down to a shell block "result" which executed the the
>>> above commands. I checked the results and started to remove the tangle
>>> object from one block and added it to the next block. Kind of tiring
>>> if you have several dozen of blocks.
>>> Guess you can see from the above example the trouble of having several
>>> dozen of them and then tangeling them one by one and execute the
>>> result block ;)
>>>
>>> I tried to make it more easy by giving the shell block a name and call
>>> it under each java code block. This would save me the time going up
>>> and down in my file.
>>>
>>> #+call: result()
>>>
>>> However, I noticed that the result update was always done at the first
>>> appearances of the call , like under the first java code block but not
>>> at the desired code block?!
>>> if you fold all together it would look like
>>>
>>> /-----------------------------------------------/
>>> #+BABEL: :mkdirp t
>>>
>>> * Coursework 1
>>> ** StudentID1
>>> #+BEGIN_SRC java
>>> #+call: result()
>>>
>>> #+results: result
>>> : 100.0
>>>
>>> ** StudentID2
>>> #+BEGIN_SRC java :tangle foo/Bar.java
>>> #+call: result()
>>>
>>> ** Result
>>> #+srcname: result
>>> /-----------------------------------------------/
>>>
>>> Calling the second call function updates the result on the first!
>>>
>>> Anyhow, I guess having it working with a ob-java.el minimal system
>>> would be the most easiest. Simply type C-c C-c and it would be done.
>>>
>>> Would be very glad if you could help me to get this somehow working.
>>>
>>> Totti
>>>
>>>
>>
>>
>> Footnotes:
>> [1]
>> #+begin_src emacs-lisp
>> (defun org-babel-execute:java (body params)
>> (let* ((classname (or (cdr (assoc :classname params))
>> (error
>> "Can't compile a java block without a classname")))
>> (src-file (concat classname ".java"))
>> (full-body (org-babel-expand-body:generic body params))
>> (compile
>> (progn (with-temp-file src-file (insert full-body))
>> (org-babel-eval (concat "javac " src-file) ""))))
>> ((lambda (results)
>> (org-babel-reassemble-table
>> (if (member "vector" (cdr (assoc :result-params params)))
>> (let ((tmp-file (org-babel-temp-file "c-")))
>> (with-temp-file tmp-file (insert results))
>> (org-babel-import-elisp-from-file tmp-file))
>> (org-babel-read results))
>> (org-babel-pick-name
>> (cdr (assoc :colname-names params)) (cdr (assoc :colnames params)))
>> (org-babel-pick-name
>> (cdr (assoc :rowname-names params)) (cdr (assoc :rownames params)))))
>> (org-babel-eval (concat "java " classname) ""))))
>> #+end_src
>>
>> [2] hello-java.org

-- 
Eric Schulte
http://cs.unm.edu/~eschulte/

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: How-to evaluate Java-snippets in org-mode/org-babel?
  2011-07-18 14:53             ` Eric Schulte
@ 2011-07-28 10:49               ` Torsten Wagner
  2011-07-29  0:06                 ` Eric Schulte
  0 siblings, 1 reply; 17+ messages in thread
From: Torsten Wagner @ 2011-07-28 10:49 UTC (permalink / raw)
  To: Eric Schulte; +Cc: Org Mode Mailing List

[-- Attachment #1: Type: text/plain, Size: 2671 bytes --]

Hi Eric,

some feedback of first testing weeks....
It works well on my desktop machine, I tried to do the same on my 
laptop. However, I faced the problem that java did not find the files.
I tried everything. Made sure using the same java and emacs version 
(same linux distro, same test file). Copied over .emacs config files, 
compared the versions of ob-java.el on both machines. I couldn't figure 
out why it works with one and does not work on the other machine.
Somehow the classpath seems to differ buy I do not see where to change 
it in the correct way.

I hacked a bit into ob-java.el file. I noticed that you call the 
generated class file by creating the string

java packagename/classname

whereas packagename/classname is coming from the classname variable set 
at the source-code block.

In the earlier attempt we used

java -cp . packagename.classname

which makes sure the classpath is set to the local directory.

I hacked this into your code and now it works on the laptop too (to say 
this modification works on both desktop and laptop). It might be worse 
to add, since the classpath seems to be set depending on many parameters 
(distro, other java ides, local user settings, etc.). Thus, many might 
observe problems at this point.

Please find a patch attached.

Even better would be to add an option to set the classpath but my elisp 
skills are to poor for that.... but would like to see how to do it ;)
#+BABEL: :classpath "."
where it might be the local directory by default?!

Beside of that I noticed that there might be a need for a plain-text 
mode.. sounds funny, but maybe sometimes someone need auxiliary files 
which are not executed by themselves. Sure I could use simply a shell 
session and echo into a file, or tangle it,  but I guess it might be 
more elegant to have a, "only-paste-this-into-this-file-execute"

#+BEGIN_SRC: plain :filename folder/folder/testdata.csv
1, 2
2, 4
3, 8
#+END_SRC
to generate /folder/folder/testdata.csv which contains the data in the 
block.

Because sometimes, one might need to call external programs or programs 
in code-blocks are supposed to read data from files instead from within 
org-babel.

In general this could be the standard feature for all unrecognised 
languages. Probably with a warning that the required 
compiler/interpreter is not configured. However, this would allow people 
at least to get the source files which they could use externally. The 
difference to tangle would be that one can decide block by block what to 
execute. E.g., I could have several of the above "testdata" blocks and 
simply executing one of them would change the input for later code blocks.

Totti




[-- Attachment #2: java_classpath.patch --]
[-- Type: text/x-patch, Size: 1177 bytes --]

From 9853070846e98c2a14452e51c8756611aa145363 Mon Sep 17 00:00:00 2001
From: Torsten <torsten.wagner@gmail.com>
Date: Thu, 28 Jul 2011 19:27:08 +0900
Subject: [PATCH] resolve problems with wrong classpath

---
 lisp/ob-java.el |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/lisp/ob-java.el b/lisp/ob-java.el
index 8595a18..cee797a 100644
--- a/lisp/ob-java.el
+++ b/lisp/ob-java.el
@@ -50,7 +50,7 @@
 	 (compile
 	  (progn (with-temp-file src-file (insert full-body))
 		 (org-babel-eval
-		  (concat org-babel-java-compiler " " src-file) ""))))
+		  (concat org-babel-java-compiler " -cp . " src-file) ""))))
     ;; created package-name directories if missing
     (unless (or (not packagename) (file-exists-p packagename))
       (make-directory packagename 'parents))
@@ -65,7 +65,7 @@
 	 (cdr (assoc :colname-names params)) (cdr (assoc :colnames params)))
 	(org-babel-pick-name
 	 (cdr (assoc :rowname-names params)) (cdr (assoc :rownames params)))))
-     (org-babel-eval (concat org-babel-java-command " " classname) ""))))
+     (org-babel-eval (concat org-babel-java-command " -cp . " classname) ""))))
 
 (provide 'ob-java)
 
-- 
1.7.6


^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: How-to evaluate Java-snippets in org-mode/org-babel?
  2011-07-28 10:49               ` Torsten Wagner
@ 2011-07-29  0:06                 ` Eric Schulte
  0 siblings, 0 replies; 17+ messages in thread
From: Eric Schulte @ 2011-07-29  0:06 UTC (permalink / raw)
  To: Torsten Wagner; +Cc: Org Mode Mailing List

Torsten Wagner <torsten.wagner@gmail.com> writes:

> Hi Eric,
>
> some feedback of first testing weeks....
> It works well on my desktop machine, I tried to do the same on my
> laptop. However, I faced the problem that java did not find the files.
> I tried everything. Made sure using the same java and emacs version
> (same linux distro, same test file). Copied over .emacs config files,
> compared the versions of ob-java.el on both machines. I couldn't
> figure out why it works with one and does not work on the other
> machine.
> Somehow the classpath seems to differ buy I do not see where to change
> it in the correct way.
>
> I hacked a bit into ob-java.el file. I noticed that you call the
> generated class file by creating the string
>
> java packagename/classname
>
> whereas packagename/classname is coming from the classname variable
> set at the source-code block.
>
> In the earlier attempt we used
>
> java -cp . packagename.classname
>
> which makes sure the classpath is set to the local directory.
>
> I hacked this into your code and now it works on the laptop too (to
> say this modification works on both desktop and laptop). It might be
> worse to add, since the classpath seems to be set depending on many
> parameters (distro, other java ides, local user settings, etc.). Thus,
> many might observe problems at this point.
>
> Please find a patch attached.
>
> Even better would be to add an option to set the classpath but my
> elisp skills are to poor for that.... but would like to see how to do
> it ;)
> #+BABEL: :classpath "."
> where it might be the local directory by default?!
>

I've just pushed up a slightly different solution which involves two new
header arguments
- :cmpflag can be used to pass flags to the java compiler (e.g., "cp .")
- :cmdline can be used to pass flags to the java executable (e.g., "cp .")

So, the following could be used to get the behavior resulting from your
patch using these flags.

#+begin_src emacs-lisp
(setq org-babel-default-header-args:java '((:cmpflag "-cp .") (:cmdline "-cp .")))
#+end_src

This solution somehow seems more flexible in that it won't force a
classpath value on users and will allow for passing other flags to the
java runtime.

>
> Beside of that I noticed that there might be a need for a plain-text
> mode.. sounds funny, but maybe sometimes someone need auxiliary files
> which are not executed by themselves. Sure I could use simply a shell
> session and echo into a file, or tangle it,  but I guess it might be
> more elegant to have a, "only-paste-this-into-this-file-execute"
>
> #+BEGIN_SRC: plain :filename folder/folder/testdata.csv
> 1, 2
> 2, 4
> 3, 8
> #+END_SRC
> to generate /folder/folder/testdata.csv which contains the data in the
> block.
>

This is currently possible with tangling, e.g.,

#+begin_src text :tangle somewhere.txt
  This will still tangle out to a file, and it opens in text mode, which
  may be nice.  
#+end_src

Thanks for the feedback and the patch -- Eric

>
> Because sometimes, one might need to call external programs or
> programs in code-blocks are supposed to read data from files instead
> from within org-babel.
>
> In general this could be the standard feature for all unrecognised
> languages. Probably with a warning that the required
> compiler/interpreter is not configured. However, this would allow
> people at least to get the source files which they could use
> externally. The difference to tangle would be that one can decide
> block by block what to execute. E.g., I could have several of the
> above "testdata" blocks and simply executing one of them would change
> the input for later code blocks.
>
> Totti
>
>
>
>

-- 
Eric Schulte
http://cs.unm.edu/~eschulte/

^ permalink raw reply	[flat|nested] 17+ messages in thread

end of thread, other threads:[~2011-07-29  0:08 UTC | newest]

Thread overview: 17+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-07-05  7:01 How-to evaluate Java-snippets in org-mode/org-babel? Torsten Wagner
2011-07-05  9:07 ` Eric S Fraga
2011-07-05  9:22   ` Eric S Fraga
2011-07-05 13:48   ` Jason F. McBrayer
2011-07-06 12:36     ` Torsten Wagner
2011-07-06 13:26     ` Eric Schulte
2011-07-06 15:14       ` Jason F. McBrayer
2011-07-06 17:11         ` Torsten Wagner
2011-07-06 19:15         ` Eric S Fraga
2011-07-06 12:28   ` Torsten Wagner
2011-07-06 13:38     ` Eric Schulte
2011-07-14  5:42       ` Torsten Wagner
2011-07-15 15:00         ` Eric Schulte
2011-07-18  1:31           ` Torsten Wagner
2011-07-18 14:53             ` Eric Schulte
2011-07-28 10:49               ` Torsten Wagner
2011-07-29  0:06                 ` Eric Schulte

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).