[groovy-user] Very simple script failing when run from jar

33 views
Skip to first unread message

polinast...@yahoo.com

unread,
Mar 16, 2010, 12:28:19 PM3/16/10
to us...@groovy.codehaus.org
I have a one line script in a file Test.java

Test.java:
  println "Hello world"


It is packaged in a jar file with the following manifest:

Manifest-Version: 1.0
Ant-Version: Apache Ant 1.6.5
Created-By: 1.5.0_09-b01 (Sun Microsystems Inc.)
Main-Class: Test
Build-Time: 2010/03/16 12:14


I am getting the following error:

java -Xbootclasspath/a:lib\groovy-all-1.7.0.jar; -jar lib\tal-dealbuilder.jar

Exception in thread "main" java.lang.NullPointerException
        at org.codehaus.groovy.runtime.callsite.CallSiteClassLoader.loadClass(CallSiteClassLoader.java:45)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at org.codehaus.groovy.reflection.ClassLoaderForClassArtifacts.loadClass(ClassLoaderForClassArtifacts.java:43)
        at java.lang.ClassLoader.loadClassInternal(Unknown Source)
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClass(Unknown Source)
        at org.codehaus.groovy.reflection.ClassLoaderForClassArtifacts.define(ClassLoaderForClassArtifacts.java:27)
        at org.codehaus.groovy.reflection.ClassLoaderForClassArtifacts$1.run(ClassLoaderForClassArtifacts.java:71)
        at org.codehaus.groovy.reflection.ClassLoaderForClassArtifacts$1.run(ClassLoaderForClassArtifacts.java:69)
        at java.security.AccessController.doPrivileged(Native Method)
        at org.codehaus.groovy.reflection.ClassLoaderForClassArtifacts.defineClassAndGetConstructor(ClassLoaderForClassArtifacts.java:69)
        at org.codehaus.groovy.runtime.callsite.CallSiteGenerator.compileStaticMethod(CallSiteGenerator.java:239)
        at org.codehaus.groovy.reflection.CachedMethod.createStaticMetaMethodSite(CachedMethod.java:268)
        at org.codehaus.groovy.runtime.callsite.StaticMetaMethodSite.createStaticMetaMethodSite(StaticMetaMethodSite.java:111)
        at groovy.lang.MetaClassImpl.createStaticSite(MetaClassImpl.java:2998)
        at org.codehaus.groovy.runtime.callsite.CallSiteArray.createCallStaticSite(CallSiteArray.java:59)
        at org.codehaus.groovy.runtime.callsite.CallSiteArray.createCallSite(CallSiteArray.java:144)
        at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:40)
        at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:117)
        at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:129)
        at Test.main(Test.groovy)
 
"He that lives upon hope will die fasting.... He that falls in love with himself will have no rivals."
~~ Benjamin Franklin

Guillaume Laforge

unread,
Mar 16, 2010, 12:39:21 PM3/16/10
to us...@groovy.codehaus.org
Curious why do you put the Groovy jar in the bootclasspath? (and not
just on the "normal" classpath)

--
Guillaume Laforge
Groovy Project Manager
Head of Groovy Development at SpringSource
http://www.springsource.com/g2one

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email


John Hartnup

unread,
Mar 16, 2010, 12:45:06 PM3/16/10
to us...@groovy.codehaus.org
On 16 March 2010 16:39, Guillaume Laforge <glaf...@codehaus.org> wrote:
> Curious why do you put the Groovy jar in the bootclasspath? (and not
> just on the "normal" classpath)

.. and why you've given Groovy file a .java extension.


--
"There is no way to peace; peace is the way"

polinast...@yahoo.com

unread,
Mar 16, 2010, 1:18:18 PM3/16/10
to us...@groovy.codehaus.org
BTW, this script works fine when run from eclipse using the groovy plug-in.  And I can get past this error when I use groovy-all-1.5.5.jar, but then I get an error in scriptom

Exception in thread "main" java.lang.ExceptionInInitializerError
        at org.codehaus.groovy.scriptom.Scriptom.initMTA(Scriptom.java:238)
...
Caused by: java.lang.NullPointerException
        at java.util.ResourceBundle.getBundle(Unknown Source)
        at com.jacob.com.LibraryLoader.loadJacobLibrary(LibraryLoader.java:130)
        at com.jacob.com.ComThread.<clinit>(ComThread.java:167)



From: "polinast...@yahoo.com" <polinast...@yahoo.com>
To: us...@groovy.codehaus.org
Sent: Tue, March 16, 2010 12:28:19 PM
Subject: Very simple script failing when run from jar

polinast...@yahoo.com

unread,
Mar 16, 2010, 3:07:08 PM3/16/10
to us...@groovy.codehaus.org
I do not get the NullPointerException from CallSiteClassLoader when I used groovy-all-1.5.5.jar, but this version of groovy causes an error in the scriptom library.  My classes load and run just fine when I run this from eclipse using groovy-all-1.7.0.jar.  When I move them out (regardless of whether I run them from a jar or by putting the .class files in the classpath) I get the NPE from CallSiteClassLoader.

I made sure that the order of the jars in my command line classpath matched that of the eclipse project.

groovy 1.5.5 is incompatible with scriptom 1.6.0 (and 1.5.4b11).  And groovy 1.7.0 does not even load a trivial Test class with hello world.  I am using the groovy-all-1.7.0.jar that came from the Groovy eclipse plug-in.

> Curious why do you put the Groovy jar in the bootclasspath? (and not
> just on the "normal" classpath)

I am loading my classes from a jar file. When I just use classpath rather than Xbootclasspath, I get the following error:
CLASSPATH=lib\scriptom-1.6.0.jar;lib\jacob-1.14.3.jar;lib\log4j.jar;lib\jtds-1.2.jar;lib\groovy-all-
1.5.5.jar;lib\commons-cli-1.2.jar;.
java -Xbootclasspath/a:lib\scriptom-1.6.0.jar;lib\jacob-1.14.3.jar;lib\log4j.jar;lib\jtds-1.2.jar;li
b\groovy-all-1.5.5.jar;lib\commons-cli-1.2.jar;. -jar lib\tal-dealbuilder.jar
Exception in thread "main" java.lang.NoClassDefFoundError: groovy/lang/Script

        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClass(Unknown Source)
        at java.security.SecureClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.access$000(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClassInternal(Unknown Source)
Caused by: java.lang.ClassNotFoundException: groovy.lang.Script
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClassInternal(Unknown Source)
        ... 12 more
Could not find the main class: Test. Program will exit.

 
"He that lives upon hope will die fasting.... He that falls in love with himself will have no rivals."
~~ Benjamin Franklin



From: Guillaume Laforge <glaf...@codehaus.org>
To: us...@groovy.codehaus.org
Sent: Tue, March 16, 2010 12:39:21 PM
Subject: Re: [groovy-user] Very simple script failing when run from jar

polinast...@yahoo.com

unread,
Mar 16, 2010, 3:59:21 PM3/16/10
to us...@groovy.codehaus.org
Sorry, the file is named Test.groovy not Test.java.


From: John Hartnup <john.h...@gmail.com>
To: us...@groovy.codehaus.org
Sent: Tue, March 16, 2010 12:45:06 PM

Subject: Re: [groovy-user] Very simple script failing when run from jar

polinast...@yahoo.com

unread,
Mar 16, 2010, 5:28:48 PM3/16/10
to us...@groovy.codehaus.org
Does anyone know how to run a groovy script from a jar?  I have done this in the past with groovy-all-1.5.5.jar, but it fails with even the simplest script for groovy-all-1.7.0.jar.
 

Samuel Doyle

unread,
Mar 16, 2010, 8:10:38 PM3/16/10
to us...@groovy.codehaus.org
Hello,

I'm attempting to execute JUnit 4 tests using the org.junit.runner.JUnitCore.runClasses utility for commandline execution.
This is done from using the groovy ant task e.g.

<groovy classpathref="groovy.test.classpath">
def basedir = project.references.groovytests.getDirectoryScanner().basedir
project.references.groovytests.getDirectoryScanner().getIncludedFiles().each {
    println "Executing: ${it}"
    def testClass = loader.parseClass(new File("${basedir}/${it}"))
     org.junit.runner.JUnitCore.runClasses(testClass)
}
</groovy>

I get the javax.persistence.PersistenceException: No Persistence provider for EntityManager named ... exception when trying to create an EntityManager and I know the classpath, classes and persistence.xml are configured properly.

Has anyone encountered an issue like this before?

Tres Finocchiaro

unread,
Mar 17, 2010, 10:05:47 PM3/17/10
to us...@groovy.codehaus.org
I'm assuming you are in the right folder when you try to run it?


> java -Xbootclasspath/a:lib\groovy-all-1.7.0.jar; -jar lib\tal-dealbuilder.jar

Because lib\ is relative.  C:\folder\lib\ would be explicit.

-Tres
--
- Tres.Fin...@gmail.com

polinast...@yahoo.com

unread,
Mar 18, 2010, 1:46:04 PM3/18/10
to us...@groovy.codehaus.org
Thanks for the suggestion, I am in the right folder. It was easy enough to test an explicit path, but that was not the problem.

This is about as simple a script as you could make
    println "Hello, world"

It runs fine with version 1.5.5, but not with 1.7.0 (or 1.7.1).

I tried this again with 1.7.1 and the error changed.  I think the problem lies with DGM.  There was a change relative to this for 1.7.1 and the error message changed (but it is still throwing a NullPointerException).  The exception is thrown because the ClassLoader is null (see the following code snippet):

  ClassLoader loader = DgmMethodRecord.class.getClassLoader();
  DataInputStream in =
     new DataInputStream(new BufferedInputStream(loader.getResourceAsStream("META-INF/dgminfo")));


Apparently, the class loader is null when the script is loaded from a java command line.  It does not matter whether the JVM is 1.5 or 1.6.  I tried putting groovy-all.jar on both classpath and bootclasspath. This does not matter.  I repeat, it runs fine with version 1.5.5, but not with 1.7.0 (or 1.7.1).

1.7.1 error message:
C:\tal-dealbuilder>dealbuilder.bat

GROOVY_HOME=
PATH=
CLASSPATH=lib\groovy-all-1.7.1.jar;bin;.

c:\progra~1\Java\jre6\bin\java -Xbootclasspath/a:lib\groovy-all-1.7.1.jar -cp lib\groovy-all-1.7.1.jar;bin;. Test

java.lang.NullPointerException
        at org.codehaus.groovy.reflection.GeneratedMetaMethod$DgmMethodRecord.loadDgmInfo(GeneratedMetaMethod.java:152)
        at org.codehaus.groovy.runtime.metaclass.MetaClassRegistryImpl.registerMethods(MetaClassRegistryImpl.java:140)
        at org.codehaus.groovy.runtime.metaclass.MetaClassRegistryImpl.<init>(MetaClassRegistryImpl.java:82)
        at org.codehaus.groovy.runtime.metaclass.MetaClassRegistryImpl.<init>(MetaClassRegistryImpl.java:60)
        at groovy.lang.GroovySystem.<clinit>(GroovySystem.java:29)
        at org.codehaus.groovy.runtime.InvokerHelper.<clinit>(InvokerHelper.java:49)
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Unknown Source)
        at Test.class$(Test.groovy)
        at Test.main(Test.groovy)

C:\tal-dealbuilder>dealbuilder.bat

GROOVY_HOME=
PATH=
CLASSPATH=lib\groovy-all-1.7.0.jar;bin;.

java -Xbootclasspath/a:lib\groovy-all-1.7.0.jar;bin;. -jar lib\tal-dealbuilder.jar

Exception in thread "main" java.lang.NullPointerException
        at org.codehaus.groovy.runtime.callsite.CallSiteClassLoader.loadClass(CallSiteClassLoader.java:45)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at org.codehaus.groovy.reflection.ClassLoaderForClassArtifacts.loadClass(ClassLoaderForClassArtifacts.java:43)
        at java.lang.ClassLoader.loadClassInternal(Unknown Source)
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClass(Unknown Source)
        at org.codehaus.groovy.reflection.ClassLoaderForClassArtifacts.define(ClassLoaderForClassArtifacts.java:27)
        at org.codehaus.groovy.reflection.ClassLoaderForClassArtifacts$1.run(ClassLoaderForClassArtifacts.java:71)
        at org.codehaus.groovy.reflection.ClassLoaderForClassArtifacts$1.run(ClassLoaderForClassArtifacts.java:69)
        at java.security.AccessController.doPrivileged(Native Method)
        at org.codehaus.groovy.reflection.ClassLoaderForClassArtifacts.defineClassAndGetConstructor(ClassLoaderForClassArtifacts.java:69)
        at org.codehaus.groovy.runtime.callsite.CallSiteGenerator.compileStaticMethod(CallSiteGenerator.java:239)
        at org.codehaus.groovy.reflection.CachedMethod.createStaticMetaMethodSite(CachedMethod.java:268)
        at org.codehaus.groovy.runtime.callsite.StaticMetaMethodSite.createStaticMetaMethodSite(StaticMetaMethodSite.java:111)
        at groovy.lang.MetaClassImpl.createStaticSite(MetaClassImpl.java:2998)
        at org.codehaus.groovy.runtime.callsite.CallSiteArray.createCallStaticSite(CallSiteArray.java:59)
        at org.codehaus.groovy.runtime.callsite.CallSiteArray.createCallSite(CallSiteArray.java:144)
        at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:40)
        at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:117)
        at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:129)
        at Test.main(Test.groovy)



From: Tres Finocchiaro <tres.fin...@gmail.com>
To: us...@groovy.codehaus.org
Sent: Wed, March 17, 2010 10:05:47 PM

Subject: Re: [groovy-user] Very simple script failing when run from jar

polinast...@yahoo.com

unread,
Mar 18, 2010, 2:10:27 PM3/18/10
to us...@groovy.codehaus.org
I used to be able to jar up my compiled groovy classes and run them from a command line:
 
java
-Xbootclasspath/a:lib\groovy-all-1.5.5.jar -cp somepath -jar myjar.jar

But this capability is now broken in Groovy 1.7.0 due to the attempt to preload DGM methods. This will fail for any compiled groovy class -- any class whatsoever -- at line 152 of GeneratedMetaMethod.java.
  ClassLoader loader = DgmMethodRecord.class.getClassLoader();
  DataInputStream in =
     new DataInputStream(new BufferedInputStream(loader.getResourceAsStream("META-INF/dgminfo")));



Sent: Thu, March 18, 2010 1:46:04 PM
Subject: Re: [groovy-user] Very simple script failing when run from jar -- problem with DGM loading

polinast...@yahoo.com

unread,
Mar 18, 2010, 2:25:36 PM3/18/10
to us...@groovy.codehaus.org
I created a bug report for this issue:

GROOVY-4109: NullPointerException: no loader in loadDgmInfo method in GeneratedMetaMethod
 
"He that lives upon hope will die fasting.... He that falls in love with himself will have no rivals."
~~ Benjamin Franklin

Sent: Thu, March 18, 2010 2:10:27 PM

Roshan Dawrani

unread,
Mar 18, 2010, 2:38:52 PM3/18/10
to us...@groovy.codehaus.org
I think when you run a main-class from the jar, "-cp" classpath is ignored - so putting the groovy jar there is no use.

Putting the issue related to putting groovy jar on the bootclasspath aside for a bit, you can run the compiled script class from the jar simply as "java -jar Test.jar" with a manifest like below:

--------------------------------------------------------
Manifest-Version: 1.0
Created-By: 1.5.0_04 (Sun Microsystems Inc.)
Main-Class: Test
Class-Path: lib/groovy-all-1.7.2-SNAPSHOT.jar
--------------------------------------------------------

Roshan Dawrani

unread,
Mar 18, 2010, 3:16:07 PM3/18/10
to us...@groovy.codehaus.org
On Thu, Mar 18, 2010 at 11:16 PM, <polinast...@yahoo.com> wrote:
Apparently, the class loader is null when the script is loaded from a java command line.  It does not matter whether the JVM is 1.5 or 1.6. 

Also, the classloader is not null because the script is loaded from a java command, it is null because the groovy class is loaded by the bootstrap class loader (because u use -Xbootclasspath/a:lib\groovy-all-1.7.1.jar). Here is the relevant javadoc of Class#getClassLoader() -

    /**
     * Returns the class loader for the class.  Some implementations may use
     * null to represent the bootstrap class loader. This method will return
     * null in such implementations if this class was loaded by the bootstrap
     * class loader.
     */

polinast...@yahoo.com

unread,
Mar 18, 2010, 7:13:21 PM3/18/10
to us...@groovy.codehaus.org
Thank you very much.  That answered my question. I am using ant's manifestclasspath operator to build the manifest's class path.

    <!-- Libraries -->
    <property name="lib.commons.cli" value="commons-cli-1.2.jar"/>
    <property name="lib.jacob"       value="jacob-1.14.3.jar"/>
    <property name="lib.jtds"        value="jtds-1.2.jar"/>
    <property name="lib.log4j"       value="log4j.jar"/>
    <property name="lib.scriptom"    value="scriptom-1.6.0.jar"/>
    <property name="lib.groovy"      value="groovy-all-1.7.1.jar"/>

    <path id="path.libraries">
        <fileset id="libraries" dir="${dir.lib}/ext">
            <include name="${lib.commons.cli}" />
            <include name="${lib.groovy}"      />
            <include name="${lib.jacob}"       />
            <include name="${lib.jtds}"        />
            <include name="${lib.log4j}"       />
            <include name="${lib.scriptom}"    />
        </fileset>
    </path>

    <manifestclasspath property="jar.classpath" jarfile="${dir.lib}/${ant.project.name}.jar">
        <classpath>
            <pathelement path="." />
            <fileset refid="libraries"/>
        </classpath>
    </manifestclasspath>
    <echo>jar.classpath: ${jar.classpath}</echo>

    <target name="package" depends="compile">
        <jar jarfile="${dir.lib}/${ant.project.name}.jar">
            <manifest>
                <attribute name="Class-Path" value="${jar.classpath}" />
                <attribute name="Main-Class" value="Test" />
                <attribute name="Built-By" value="${user.name}"/>
                <attribute name="Build-Time" value="${BuildTime}"/>
            </manifest>
            <fileset dir="${dir.bin}"/>
            <fileset dir="${dir.config}"/>
        </jar>
    </target>



 
"He that lives upon hope will die fasting.... He that falls in love with himself will have no rivals."
~~ Benjamin Franklin



From: Roshan Dawrani <roshan...@gmail.com>
To: us...@groovy.codehaus.org
Sent: Thu, March 18, 2010 2:38:52 PM
Reply all
Reply to author
Forward
0 new messages