*
The moose likes Groovy and the fly likes Call groovy program from Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Languages » Groovy
Bookmark "Call groovy program from Java" Watch "Call groovy program from Java" New topic
Author

Call groovy program from Java

Brian Oleksa
Greenhorn

Joined: Jun 05, 2007
Posts: 15

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

Joined: May 14, 2008
Posts: 5812
    
    7

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.


JBoss In Action
Brian Oleksa
Greenhorn

Joined: Jun 05, 2007
Posts: 15

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

Joined: Feb 05, 2003
Posts: 4727

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


A good workman is known by his tools.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41127
    
  45
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.


Ping & DNS - my free Android networking tools app
Peter Johnson
author
Bartender

Joined: May 14, 2008
Posts: 5812
    
    7

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

Joined: May 14, 2008
Posts: 5812
    
    7

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

Joined: Apr 13, 2009
Posts: 96
This is along the lines of what you're looking for:
http://groovy.codehaus.org/Embedding+Groovy
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Call groovy program from Java
 
Similar Threads
problem with Runtime getRuntime() exec(command)
Simple database
whats the best book on Servlets
Who said Java is easy?
insert checked checkbox into table