Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

Execute rebol from java

 [1/11] from: arolls:idatam:au at: 3-Jan-2002 17:25


Does anyone have any java code to execute a rebol script? I am having serious withdrawal symptoms today. I am forced to use java at work here, and I just came across a problem that would have been short, elegant and simple in rebol, and almost downright impossible in java (it seems). I was advised by a more experienced programmer here on all the benefits of adding three more classes, when all I wanted to do was navigate a simple array/block with a known structure and get some strings out. (Gnashing of teeth.) Sorry, I will try to deal with it. I am going to go looking for a nice parser for java (if there is one). Anton.

 [2/11] from: rotenca:telvia:it at: 3-Jan-2002 17:34


Hi Anton,
> I am having serious withdrawal symptoms today. > I am forced to use java at work here, and I just came across > a problem that would have been short, elegant and simple in > rebol, and almost downright impossible in java (it seems).
Uh! They remember me some Visual Basic corporate fans. None of Visual Basic, Java, Windows, Linux or Rebol are able to change the minds of these people. --- Ciao Romano

 [3/11] from: hallvard:ystad:helpinhand at: 3-Jan-2002 18:01


Dixit Anton Rolls (07.25 03.01.2002):
>Does anyone have any java code to execute a rebol script?
How about this (copied from a class I had laying around): try { Process p = Runtime.getRuntime().exec("Your command here"); BufferedReader pi = new BufferedReader (new InputStreamReader(p.getInputStream())); BufferedReader pe = new BufferedReader (new InputStreamReader(p.getErrorStream())); while ((s = pi.readLine()) != null) { System.out.println(s + "\n"); } while ((s = pe.readLine()) != null) { System.out.println(s + "\n"); } } catch (Exception e) { e.printStackTrace(); }
>I am having serious withdrawal symptoms today. >I am forced to use java at work here, and I just came across >a problem that would have been short, elegant and simple in >rebol, and almost downright impossible in java (it seems).
This reminds me of the proxyserver I wrote in 40 lines of rebol code to replace 20k / 5 classes of java a few weeks ago... Good luck! ~H

 [4/11] from: nitsch-lists:netcologne at: 3-Jan-2002 18:39


RE: [REBOL] Execute rebol from java Hi Anton [arolls--idatam--com--au] wrote:
> Does anyone have any java code to execute a rebol script? > I am having serious withdrawal symptoms today.
<<quoted lines omitted: 9>>
> I am going to go looking for a nice parser for java (if there is > one).
reviseted java a bit. javacc, coco are compiler-compilers, similar to parse. also there are regular-expressions somewhere near the jpython (and inbuild in 1.4 or that?). to use rebol, below is some playing which starts a rebol-script and fills its output in a vector of lines. but '\n' as line-delemiter is hardcoded, forgot the name for the second windows-char.. hope it helps a bit.
> Anton.
-Volker
> --
[ REBOL [title: {do me to build files..}] make-dir/deep %callrebol-project/volker/callrebol/ WRITE %callrebol-project/callreboldemo.r {rebol[title: file: %callreboldemo.r ] inform layout[ button "send"[hide-popup print ta/text] ta: area "Hello Java!" ] } WRITE %callrebol-project/CallRebolDemo.java { import volker.callrebol.RebolConnection; import java.io.IOException; /** * Demonstrate use of package volker.callrebol. * * @author volker * @version 0.1 */ public class CallRebolDemo { // instance variables static RebolConnection rebolConnection; /** * Constructor for objects of class Demo */ public CallRebolDemo() { // initialise instance variables } /** * the Demo starts here * * @param args command line */ public static void main(String[] args) throws IOException { // put your code here rebolConnection = new RebolConnection(); /*my ide start in the ".."-directory, so "callrebol-project" points ot the root-package with volker.callrebol in it. ugh. configuring around..*/ rebolConnection.scriptAndArguments ="callrebol-project/callreboldemo.r"; rebolConnection.runRebol(); rebolConnection.readAll(); System.out.println(rebolConnection.output.toString()); System.exit(0); } } } WRITE %callrebol-project/volker/callrebol/RebolConnection.java {package volker.callrebol; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.Vector; /** * Call a rebol-job, return console-output. * * @author Volker * @version 0.1 */ public class RebolConnection { //parameter variables /**set script and arguments for command-line*/ public String scriptAndArguments; /**here is the script-output, parsed into lines*/ public final Vector output=new Vector(); /**change to point to your rebol*/ public String executable="/usr/local/bin/rebol"; // instance variables Process rebolProcess; Runtime runtime=Runtime.getRuntime(); InputStream inputStream; StringBuffer stringBuffer=new StringBuffer(); /** * Constructor for objects of class RebolConnection */ public RebolConnection() { } /** * Start rebol based on args in public variables. * **/ public void runRebol() throws IOException { rebolProcess = runtime.exec( new String[]{executable,scriptAndArguments}); inputStream = rebolProcess.getInputStream(); } /** * parse the input into lines and fill them in this.output. * can run in another thread. * attention, lines-delemiter is coded platform-dependent, * and needed as last char in file. * * @param input an input stream **/ public void readAll() throws IOException { InputStream input=this.inputStream; int got; while(-1 != (got=input.read())){ if('\n'==(char)got){ String s = stringBuffer.toString(); stringBuffer.setLength(0); output.add(s); } else{stringBuffer.append((char)got);} } } } } ]

 [5/11] from: arolls:idatam:au at: 4-Jan-2002 15:13


Ah fantastic guys! I will try it out... But I still don't get to use rebol in the context that I was gnashing my teeth in. But maybe this code will allow me to sneak it in somewhere... he he... Anton.

 [6/11] from: arolls:idatam:au at: 4-Jan-2002 21:24


Volker, I got your project working ok, but I am still struggling to see the output. I have tried squishing Hallvard's output code in place of yours, in RebolConnection.readAll method, because it looks a little more cross-platform (?) But wait! Aren't rebol line-terminators always '\n'? Then that's what you had originally! (I admit I am a bit confused where it's going.) I will see if I get anything from the console later. (I haven't used java from the console for a long time.) Thanks... Anton.

 [7/11] from: nitsch-lists:netcologne at: 4-Jan-2002 14:48


RE: [REBOL] Re: Execute rebol from java Hi Anton, ups, no, drop my version!! Hallvards is a lot better! wasnt aware the class with readLine() anymore.. Long time not really touched java.. (there was no answer for i while so i tried it ;-) i think you can drop the error-stream there and use try { Process p = Runtime.getRuntime().exec("Your command here"); BufferedReader pi = new BufferedReader (new InputStreamReader(p.getInputStream())); while ((s = pi.readLine()) != null) { //here you have a rebol-line in 's, do something with it System.out.println(s + "\n"); //show it for example } } catch (Exception e) { e.printStackTrace(); } (thank you Hallvard :) [arolls--idatam--com--au] wrote:
> Volker, > I got your project working ok, but > I am still struggling to see the output. >
usually ides have a console-output-window somewhere hidden..
> I have tried squishing Hallvard's > output code in place of yours, > in RebolConnection.readAll method, > because it looks a little more > cross-platform (?)
Yes. and shorter. and more clear and all that :) simply process the strings he outputs,
> But wait! Aren't rebol line-terminators > always '\n'?
not if you print them? converted os-dependend like with files? hmm, don't know.
> Then that's what you had originally! > (I admit I am a bit confused where
<<quoted lines omitted: 3>>
> for a long time.) > Thanks...
-Volker

 [8/11] from: arolls:idatam:au at: 7-Jan-2002 14:32


Cool, I got it all working. :) Thanks! Volker, you should add to your callreboldemo.r print "hello java" wait 5 The window was popping up and disappearing so fast I couldn't see the text at all! But then I decided to make cgi work. I noticed that the scriptAndArguments will be parsed by StringTokenizer, only on these characters: " \t\n\r\f". There is no handling there of double quotes, so if scriptAndArguments is for example "-cs list-demos/callrebol-project/callreboldemo.r", then rebol comes back with usage info as output. :-/ So I made a new String[], built from executable and scriptAndArguments (which is also now a String[]), so you can instead pass in "-cs", "list-demos/callrebol-project/callreboldemo.r" Changes in RunRebol: // build cmdarray from executable and scriptAndArguments String[] cmdarray = new String[scriptAndArguments.length + 1]; cmdarray[0] = executable; for (int i = 0; i < scriptAndArguments.length; i++) cmdarray[i + 1] = scriptAndArguments[i]; rebolProcess = runtime.exec(cmdarray); Changes in main: rebolConnection.scriptAndArguments = new String[] {"-cs", "list-demos/callrebol-project/callreboldemo.r"}; // find the directory we start in //{"-cs", "--do", "print what-dir"}; Thanks again! Mmm, it would be nice to be able to check the registry to see where rebol is installed. Actually, what is a cross-platform way to do this? Sounds many-platformed to me! But maybe there is a simple way? Rebol resolution! Also it would be nice to run rebol all the time from java, watch the error output, and see if anything ever comes out. (It could be a waste of time, though). Anton.

 [9/11] from: arolls:idatam:au at: 8-Jan-2002 15:01


Volker, I tried out javacc today, and tested it on a simple .c file, which seemed to do something (not sure what:). But then I realised that what I wanted was compose-like functionality in java. So I am thinking I will make a java class to output a .java file, then javac it, and run the new class with Class.forName("new.class");. I would prefer to do this in memory somehow, too. And if someone else has done this already, or if javac can help this kind of thing. This isn't /really/ important... I just won't feel 100% healthy programming in java until I can do composed code like rebol can... :) Of course, once I have a class that can execute composed java code (in a clean, simple way), I will be so much more powerful. ;) Anton.

 [10/11] from: nitsch-lists:netcologne at: 8-Jan-2002 13:15


RE: [REBOL] Re: Execute rebol from java Hi Anton [arolls--idatam--com--au] wrote:
> Volker, > > I tried out javacc today, and tested it on a simple > ..c file, which seemed to do something (not sure what:). > But then I realised that what I wanted was compose-like > functionality in java. >
darkly remembering a demo which added some demo-syntax to java, outputting "clean java". done with a tool packed with javacc called jjtree and a grammar for java. then there is scheme and python both compiling to java-bytecode. And Freebell on the FX5 -site. Maybe you can motivate Frank Sievertsen & RT to allow it / make it usable?
> So I am thinking I will make a java class to output a > ..java file, then javac it, and run the new class with > Class.forName("new.class");. > I would prefer to do this in memory somehow, too. > And if someone else has done this already, or if javac > can help this kind of thing. >
hum. you need javac internal for speed and ClassLoader to allow for multiple classes of the same name (like multiple times the same applet loaded). if i now could remember where i have that prototype stuff, and how to use/configure.. ;-)
> This isn't /really/ important... I just won't feel > 100% healthy programming in java until I can do composed > code like rebol can... :) >
Well, then it needs smarter syntax .I prefer the original. *g* But if you need to use/learn java, its a funny way. specially if the composing can be done with Runtime.exec(uting) rebol :)
> Of course, once I have a class that can execute > composed java code (in a clean, simple way), I will be > so much more powerful. ;) >
Hm, feedback to polish it? on or off list? the composing part here? ;-)
> Anton. > > > I was advised by a more experienced programmer here on
<<quoted lines omitted: 9>>
> > > > -Volker
-Volker

 [11/11] from: arolls:idatam:au at: 9-Jan-2002 13:09


Hi Volker,
> Hi Anton > darkly remembering a demo which added some demo-syntax to java, > outputting "clean java". > done with a tool packed with javacc called jjtree and a > grammar for java.
It's all far too complicated!! ;) They seem very useful so I'll have to check them out some more.
> then there is scheme and python both compiling to java-bytecode. > And Freebell on the FX5 -site. Maybe you can motivate > Frank Sievertsen & RT to allow it / make it usable?
I just checked out Freebell. It looks very promising. Go Frank.
> > So I am thinking I will make a java class to output a > > ..java file, then javac it, and run the new class with > > Class.forName("new.class");. > > I would prefer to do this in memory somehow, too. > > And if someone else has done this already, or if javac > > can help this kind of thing.
I nearly finished a working version yesterday. It's missing the compile stage, at the moment, out of the steps: compose, save, compile, run. The rest works ok. (It won't be hard to do.)
> hum. you need javac internal for speed and ClassLoader > to allow for multiple classes of the same name > (like multiple times the same applet loaded). > if i now could remember where i have that prototype stuff, > and how to use/configure.. ;-)
These enhancements would really make it look good. I am interested...
> > This isn't /really/ important... I just won't feel > > 100% healthy programming in java until I can do composed
<<quoted lines omitted: 3>>
> specially if the composing can be done with > Runtime.exec(uting) rebol :)
That's a good idea! But how to keep it all in one file? Can a java class know where its source file is located? Then I could embed the java code in multi-line comments, which would be better than the messy way I am currently using (array of strings - see code link below.)
> > Of course, once I have a class that can execute > > composed java code (in a clean, simple way), I will be > > so much more powerful. ;) > > Hm, feedback to polish it? > on or off list? the composing part here? ;-)
Hmm. As long as it has /something/ to do with rebol. I have to say that my experience composing with rebol is the main reason why I would try something like this. Without that experience, I doubt I would have realised how useful composing can be. I will keep the code off the list unless it contains some rebol though. The code is accessible at the bottom of this page: http://anton.idatam.com.au/index.html
> -Volker
Anton.

Notes
  • Quoted lines have been omitted from some messages.
    View the message alone to see the lines that have been omitted