[groovy-user] Problem running application packaged as a JAR files

2 views
Skip to first unread message

Alexis Wilpert

unread,
May 20, 2009, 5:10:34 AM5/20/09
to us...@groovy.codehaus.org
Hello,

I am new to Groovy and to this forum and it is possible that the problem
I explain below has been solved already or it is not a problem at all (I
am doing the wrong thing).

My problem is that I am not able to run an application packaged as a JAR
file from Groovy source files. I do not have problems doing the same
from classes compiled with the Java compiler (javac).

For testing, I used the following three files: Book.groovy,
BookApp.groovy and the manifest books.mf (the examples are taken from
the book "Groovy in Action"):

Book.groovy:

class Book {
String title
Book (String theTitle) {
title = theTitle
}
String getTitle(){
return title
}
static void main(String[] args){
}
}

---

BookApp.groovy:

public class BookApp {
public static void main(String[] args) {
Book gina = new Book('Groovy in action')
print(gina.getTitle())
}
}

---

books.mf:

Main-Class: BookApp\n

(\n means that the line is terminated as required)

---

Then I compile the source files with groovyc:

groovyc BookApp.groovy
--> FILES:
Book.class
BookApp.class

And I test and confirm that I can run them successfully with java:

java BookApp
--> OUTPUT:
Groovy in action

I build an application packaged as a JAR file:

jar cmf books.mf BookApp.jar Book.class BookApp.class
--> FILE:
BookApp.jar

I make sure that both classes are in JAR file using the jarlook.jar
utility ()

java -jar jarlook.jar BookApp.jar
--> OUTPUT:
Book.class
BookApp.class
META-INF/
META-INF/MANIFEST.MF

I checke and confirme that MANIFEST.MF contains the line:
Main-Class: BookApp

I finally try and fail to run the application running the java program
with the -jar switch:

javaw.exe -jar BookApp.jar
--> OUTPUT:
Could not find the main class: BookApp. Program will exit.

I do not know what is going on and I would appreciate your help and
advice very much.

Thanks in advance,


Alexis

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

http://xircles.codehaus.org/manage_email


Alexis Wilpert

unread,
May 20, 2009, 5:37:29 AM5/20/09
to us...@groovy.codehaus.org
I just noticed that by a classical copy&paste error (my apologies), I
left in the Book.groovy source file a definition for the main method.
However, removing it does not change anything in the problem I reported.

Alexis

Martin Stephan

unread,
May 20, 2009, 5:58:26 AM5/20/09
to us...@groovy.codehaus.org
Hi

I guess you need to put the groovy jar in there,too. Either by using JarJar or FatJar or one of the solutions posted elsewhere on this list. Simply putting the groovy-embeddable.jar inside your jar is sadly not enough.

Or, you add the (class)path to groovy to your jar call.

Regards

Martin

Jochen Theodorou

unread,
May 20, 2009, 6:36:56 AM5/20/09
to us...@groovy.codehaus.org
Martin Stephan schrieb:

> Hi
>
> I guess you need to put the groovy jar in there,too. Either by using
> JarJar or FatJar or one of the solutions posted elsewhere on this
> list. Simply putting the groovy-embeddable.jar inside your jar is
> sadly not enough.
>
> Or, you add the (class)path to groovy to your jar call.
>

If he had a classpath problem, then it should show itself as
ClassNotFoundException for a groovy runtime class. But the main class
itself was not found.

bye blackdrag

Alexis Wilpert

unread,
May 21, 2009, 1:44:20 AM5/21/09
to us...@groovy.codehaus.org
Actually, javaw.exe did not give the full list of errors (only the last
one?). When trying to run the created JAR file with java.exe, I get the
following message:

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: BookApp. Program will exit.

Notice the first line: Exception in thread "main"
java.lang.NoClassDefFoundError: groovy/lang/Script. This message
incidentally is the same one I get when trying to run the Application class
file without setting the CLASSPATH variable to point to the Groovy
embeddable JAR file. It seems that it could have to do with failing
dependencies, as Martin pointed out. However the question, if this is true,
is then: why is setting the CLASSPATH works with class files but not with
the JAR file? Is it possible to solve this problem without embedding the
Grroovy JAR file (>4MB)? By the way: I did not manage yet to embed it into
the target application, so I cannot confirm yet that embedding solves the
problem.

Greetings: Alexis

Alexis Wilpert

unread,
May 21, 2009, 5:48:50 AM5/21/09
to us...@groovy.codehaus.org
I found one solution to the problem: I must set the Class-Path attribute in
the JAR manifest to point to the groovy-all JAR file, like this:

Class-Path:
file:/C:/Program%20Files%20(x86)/Groovy/Groovy-1.6.3/embeddable/groovy-all-1.6.3.jar

The JAR File Specification speaks of "relative URLs" but the above setting
works for me. It would be nice though to be able to use environment
variables in the Class-Path attribute (like ${GROOVY_HOME}); this way the
generated JAR could be easily shared among different users.

My question did not have anything to do with Groovy itself and therefore I
apologize for eventually bothering this list with it.

Thanks: Alexis

Reply all
Reply to author
Forward
0 new messages