开发者

Cannot find class even when jar file is in working directory

I am struggling to get my Java program to run on AIX. I used Eclipse on Windows to create a runnable Jar file, jRams.jar below. I kept on getting a class not found error, until finally I put all the external libraries in the same directory.

$ ls
JAXB2_20081030.jar
JAXB2_20110601.jar
activation.jar
asjava.jar
commons-beanutils-1.8.3.jar
c开发者_JS百科ommons-beanutils-bean-collections-1.8.3.jar
commons-beanutils-core-1.8.3.jar
commons-codec-1.5.jar
commons-collections-3.2.1.jar
commons-configuration-1.6.jar
commons-digester-2.1.jar
commons-jxpath-1.3.jar
commons-lang-2.6.jar
commons-logging-1.1.1.jar
commons-logging-adapters-1.1.1.jar
commons-logging-api-1.1.1.jar
jRams.jar
jaxb-api.jar
jaxb-impl.jar
jaxb-xjc.jar
jaxb1-impl.jar
jremote.jar
jsr173_1.0_api.jar
log4j-1.2.16.jar
netty-3.2.4.Final.jar
$

Still, I get the class not found error.

$ java -jar jRams.jar
The java class is not found:  com.jbase.jremote.JRemoteException

jremote.jar definitely contains JRemoteException. Why isn't this working?

UPDATE

Thank you for your straight-to-the-point answers. I now understand the nature of a java application and a manifest file far better.

Turns out my ftp client was transferring in ASCII mode and not Binary, so the jar files were corrupt. I have learned a great deal, nonetheless.


When using the -jar option, you need to specify which jar-files should be on your class path in the manifest file. Just having the required jar files in the same directory won't do it.

Add a line in your manifest that says:

Class-Path: JAXB2_20081030.jar:JAXB2_20110601.jar:....:netty-3.2.4.Final.jar

or skip the -jar option and launch using

java -cp JAXB2_20081030.jar:....:netty-3.2.4.Final.jar:jRams.jar pkg.JRamsMain

and it should work fine.

(Note that on *nix systems, as opposed to Windows machines, the jar files in the class paths should be separated using : instead of ;.)

Further reading:

  • The Java Tutorials: Adding Classes to the JAR File's Classpath


You need to add all those JARs to the runtime CLASSPATH by adding the -classpath parameter. AIX requires you to separate the JARs using :


You will have to specify the full path(if libraries not in the same directory as jRams) or just the names of the jar file in a manifest file (If all dependency jars are in the same folder). Alternative specify the path to all the dependent jars using -cp argument.

Example (This assume every dependency is in the same directory you are executing java command from):

java -cp commons-collections-3.2.1.jar; jaxb-impl.jar; etc.. ;jRams.jar package_to_class.MyMainClass.java

Where package_to_class is example: com.myproj.example.

EDITED.


Follow these steps to add "Class-Path" to existing jar file -

  1. Create "newmanifest" file with following entry Class-Path: additional/jars

  2. Update existing jar file e.g. "classes.jar" jar --update --manifest=newmanifest --file classes.jar

  3. Inflate jar file jar -xvf classes.jar created: META-INF/ inflated: META-INF/MANIFEST.MF

  4. Verify "Class-Path" is added to MANIFEST.MF

cat META-INF/MANIFEST.MF Manifest-Version: 1.0 main-class: CLASSNAME Created-By: 15.0.2 (Oracle Corporation) Class-Path: additional/jars

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜