Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Call groovy program from Java

 
Brian Oleksa
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I thought that this would be as easy as:

MyGroovyProgram program = new MyGroovyProgram ()
program.MyGroovyMethod().

But I get an error in java saying that MyGroovyProgram cannot be found.

I am using Netbeans....the code looks to be fine. The dot operator found MyGroovyMethod() for me.....
so if it found it...why doesn't this work...??

Shouldn't it be this easy...??

Thanks in advanced.

Brian
 
Peter Johnson
author
Bartender
Posts: 5852
7
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, it is not that easy. Your Groovy class is probably in a text file - the JVM on the other hand requires a class file in the classpath. Part of what the Groovy runtime does is automatically convert Groovy source files into Java class files and add them into the classpath. So what you have to do is convince the Groovy runtime to do that for you within you Java app so that you can call the Groovy class.

You could try running your Java app with the groovy.bat or groovy.sh script. Or look into those scripts to see what options they are passing to the java command line and add those same options to your java command line.
 
Brian Oleksa
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I just tried to run my java program (which calls my groovy script) inside the groovy console and it did not like it.
I even tried to rename my java program to myprogram.groovy and I got the same problem.

all I am trying to do is use the very nice groovy XML MarkupBuilder to produce a XML file based on the data that I get from running MyProgram.java

So myprogram.java is parsing a txt file and placing the data I need into an ArrayList<JavaBeans>. I then want to take that data out of the arraylist and
create a XML file using groovys MarkupBuilder.

Thanks,
Brian

 
Marc Peabody
pie sneak
Sheriff
Posts: 4727
Mac Ruby VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"So myprogram.java is parsing a txt file and placing the data I need into an ArrayList<JavaBeans>. I then want to take that data out of the arraylist and
create a XML file using groovys MarkupBuilder."

Why not do the file parsing in Groovy? It's very well suited for that.
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Groovy supports both of the major Java scripting engines - the javax.script package (available in Java 6) and Apache BSF. Either of those can provide smooth Java/Groovy integration.
 
Peter Johnson
author
Bartender
Posts: 5852
7
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just tried to run my java program (which calls my groovy script) inside the groovy console and it did not like it.
I even tried to rename my java program to myprogram.groovy and I got the same problem.


What error message are you seeing? What is the class name? myprogram or MyProgram or some other spelling? (See http://faq.javaranch.com/java/ShowSomeEffort, especially the section on casing.) Does the class have a package? If so, did you use the full class name, including the package name, in the Groovy console?
 
Peter Johnson
author
Bartender
Posts: 5852
7
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's a simple example of running a Java class from Groovy console. I have these files:

org/peter/Util.java
org/peter/Util.class (I compiled Util.java)
org/peter/GUtil.groovy

Here is the source for Util.java:



The source code for GUtil.groovy is almost exactly the same, only the class name, Util, was changed to GUtil.

I started groovyconsole in the base directory and entered this script:



When I ran the script I got:



 
John Kimball
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is along the lines of what you're looking for:
http://groovy.codehaus.org/Embedding+Groovy
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic