Problem locating class with .jar file

I am using Studio Enterprise 8. I have created two projects, one is a java application and the other is a java library. Both the application and the library create their own packages and jar files.

My application references classes in the library package. In order to compile the application I needed to specify the library .jar file under the project/properties/libraries for the application project. It builds fine and also runs under the JSE8 environment.

My problem arises when I attempt to run the application .jar file from the command line (using "java -jar controllerapplicationproject.jar). The main window of my application comes up with gui componenents and all, but I get an error message on the dos window that says

Exception in thread "Thread-0" java.lang.NoClassDefFoundError: FGCOCP/FGCOCPEndpoint

at controllerapplication.ControllerThread.run(ControllerThread.java:126)

The FGCOCP package is the package name created by the library project and FGCOCPEndpoint is one of the classes in that project.

Note, again, that it runs fine under the JSE8 environment.

I have two questions regarding this issue:

1. Is there a way to bundle the application jar file and the library jar files together within the application jar file?

2. Is there a way to keep the two jar files separated, put them in a folder somewhere and run the application jar file with it knowing to look in the library jar file for class definitions? I've been trying this, using the -classpath parameter but it seems using java -jar expects there to be just one jar file containing all classes.

[1636 byte] By [Skrelton] at [2007-11-26 8:41:19]
# 1

> 1. Is there a way to bundle the application jar file

> and the library jar files together within the

> application jar file?

AFAIK, no. If you have two projects you will get two jar files in the end. I don't know of a way to make two different projects compile into a single jar.

> 2. Is there a way to keep the two jar files

> separated, put them in a folder somewhere and run

> the application jar file with it knowing to look in

> the library jar file for class definitions? I've

> been trying this, using the -classpath parameter but

> it seems using java -jar expects there to be just one

> jar file containing all classes.

This is done via the Class-Path entry in the application's manifest.mf file. Open the Files view, find the manifest.mf file for your project and add a new line there - Class-Path: <relative-path-to-library-jar>. Then build, move the library jar to that relative location and try java -jar again.

KSorokin at 2007-7-6 22:19:14 > top of Java-index,Development Tools,Java Tools...
# 2

I'm modifying an application imported into JSE8 as a "Existing application with java sources" (or similar) that, in its build.xml, manages to do that. The project directory layout is more or less like this:

<project_base_dir>

+-- lib

+-- nbproject

+-- org (source code is here)

+-- testarea (built JAR and ZIPs go here)

+-- Manifest.txt

\-- build.xml

And the relevant part of build.xml is this:

<target depends="clean" name="compile">

<javac source="1.4" classpath="lib/commons-codec-1.2.jar" debug="true" deprecation="true" destdir="." srcdir=".">

</javac>

</target>

<target depends="compile" name="jar">

<jar basedir="." compress="true" jarfile="testarea/${version}.jar" manifest="Manifest.txt">

<exclude name="**/*.java"/>

<exclude name="**/*.form"/>

<exclude name="Manifest.txt"/>

<exclude name="build.xml"/>

<exclude name="**/.nbattrs"/>

<exclude name="nbproject/" />

<exclude name="testarea/${version}.jar"/>

<exclude name="testarea/${version}_src.jar"/>

<exclude name="apidoc/"/>

<exclude name="cache/"/>

<!-- fileset dir="lib/"/ -->

<exclude name="*.jar"/>

<exclude name="*.zip"/>

<exclude name="*.odt"/>

</jar>

</target>

Basically, it puts everything under the base project except what is not needed (most of the content, actually). :-) This results in commons-codec-1.2.jar being included into the application JAR.

I haven't tried myself, but you could adapt the standard build.xml to do something similar.

HTH

RickieNewbie at 2007-7-6 22:19:15 > top of Java-index,Development Tools,Java Tools...
# 3

Thank you both very much for the extremely helpful information! I'm going to attempt go with the separarate jar files for now, as KSorokin explained as I'm a bit pressed for time for a demo :-)

However, once I've got some time to experiment I'm going to check into the method Rickie has suggested.

I will post follow-ups on this thread regarding whatever comes of it.

Skrelton at 2007-7-6 22:19:15 > top of Java-index,Development Tools,Java Tools...