Re: Running a Groovy script with a compiled Java class
Maarten Boekhold <[email protected]>
| Newsgroups | gmane.comp.lang.groovy.user |
|---|---|
| Message-ID | <[email protected]> |
Another way of doing this:
#!/bin/bash
DUMMY=''' '
# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4
if [ -z "$JAVA_HOME" ]; then
echo "JAVA_HOME is not set"
exit
fi
if [ ! -f "$JAVA_HOME/bin/java" ]; then
echo "JAVA_HOME does not point to a valid java installation"
exit
fi
# This was written when "groovy-all" still existed. It will
# need to be updated to include all required groovy jars
# into the CLASSPATH...
GROOVY_EMBED=/opt/groovy/lib/groovy-all-2.1.8.jar
"$JAVA_HOME"/bin/java \
-Xmx128m \
-Dprogram.name="" \
"-Dtools.jar=$JAVA_HOME/lib/tools.jar" \
"-Dscript.name=${0##*/}" \
-classpath "$GROOVY_EMBED:$JAVA_HOME/lib/tools.jar" \
org.codehaus.groovy.tools.GroovyStarter \
--main groovy.ui.GroovyMain \
"$0" $@
exit $?
' '''
println "Put your groovy script code here"
On November 23, 2021 09:27:50 Maarten Boekhold <[email protected]> wrote:
> Another neat way to do this is to embed your groovy script inside a bash
> script as per the method described in the following post:
>
> https://mail-archives.apache.org/mod_mbox/groovy-users/201506.mbox/%[email protected]%3E
>
> Maarten
>
> On November 22, 2021 16:56:55 Blake McBride <[email protected]> wrote:
>> Greetings,
>>
>> For my case, I think I have found a perfect solution. I created the
>> following bash script named run:
>>
>> -------------------------------
>>
>> #!/bin/bash
>>
>> # This script allows the running of a Groovy file in the context of the
>> # entire system.
>>
>> JAR_PATH=`dirname $0`/../lib
>> CLASSES_PATH=`dirname $0`/../build/web/WEB-INF/classes
>>
>> THE_CLASSPATH=.
>> for i in `ls $JAR_PATH/*.jar`
>> do
>> THE_CLASSPATH=${THE_CLASSPATH}:${i}
>> done
>>
>> THE_CLASSPATH=${THE_CLASSPATH}:${CLASSES_PATH}
>>
>> java -cp ${THE_CLASSPATH} groovy.ui.GroovyMain "$@"
>>
>>
>> ------------------------------------
>>
>> I am then able to run a groovy file in the context of the rest of my system
>> (this system has 9500 classes and 89 jar files). I can now simply do:
>>
>> run MyGroovyFile
>>
>> Thanks!
>>
>> Blake
>>
>>
>>
>> On Mon, Nov 22, 2021 at 6:09 AM Blake McBride <[email protected]> wrote:
>> Hi Paul,
>>
>> That worked great. Thank you!
>>
>> However, it would be very convenient if I could specify the -cp using the
>> "java -jar" method like so:
>>
>> java -jar groovy-3.0.9-indy.jar -cp . TestGroovy
>>
>> The reason is that the example I gave you is a simplified example of what I
>> am actually doing. Although the method you gave me does work in my
>> situation too, it is situationally somewhat awkward. It would be very
>> convenient if the groovy jar accepted and used the -cp command. Is there
>> any way I can do that?
>>
>> (Just FYI, I am working on https://github.com/blakemcbride/Kiss In
>> particular, see section 7 of
>> https://htmlpreview.github.io/?https://github.com/blakemcbride/Kiss/blob/master/manual/man/index.html
>> )
>>
>> Thanks!
>>
>> Blake
>>
>>
>>
>>
>> On Mon, Nov 22, 2021 at 1:19 AM Paul King <[email protected]> wrote:
>> Hi Blake,
>>
>> If you add the following line into your TestGroovy script:
>> println System.getProperty('java.class.path')
>> you will see that "." from the -cp commandline switch to java isn't
>> passed through to Groovy when using java -jar.
>>
>> You can instead use (semicolon would be the path separator on Windows):
>> java -cp /path/to/groovy/jar:. groovy.ui.GroovyMain TestGroovy
>>
>> or:
>> /path/to/groovy TestGroovy
>>
>>
>>
>> Cheers, Paul.
>>
>> On Mon, Nov 22, 2021 at 1:49 PM Blake McBride <[email protected]> wrote:
>>>
>>> Greetings,
>>>
>>> I can run a simple Groovy class (TestGroovy.groovy) without explicitly
>>> compiling it as follows:
>>>
>>> java -jar groovy-3.0.9-indy.jar TestGroovy
>>>
>>> I have a compiled Java class file named TestJava.class
>>>
>>> I am not using any package declarations. everything is in the current
>>> directory.
>>>
>>> I changed TestGroovy.groovy to call my TestJava.class file, however, this
>>> is what I am getting:
>>>
>>> $ java -jar groovy-3.0.9-indy.jar TestGroovy
>>> org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
>>> /home/blake/groovy/TestGroovy.groovy: 7: Apparent variable 'TestJava' was
>>> found in a static scope but doesn't refer to a local variable, static field
>>> or class. Possible causes:
>>> You attempted to reference a variable in the binding or an instance
>>> variable from a static context.
>>> You misspelled a classname or statically imported field. Please check the
>>> spelling.
>>> You attempted to use a method 'TestJava' but left out brackets in a place
>>> not allowed by the grammar.
>>> @ line 7, column 3.
>>> TestJava.javaMethod()
>>> ^
>>>
>>> 1 error
>>>
>>> I also tried: java -cp . -jar groovy-3.0.9-indy.jar -cp . TestGroovy
>>> With the same error.
>>>
>>> Basically, I am trying to run a Groovy file and have it call a Java class
>>> that I am supplying.
>>>
>>> I am attaching the Java and Groovy files. Sure appreciate any help.
>>>
>>> Blake McBride
>>>