This is more of a question with Java in general, but I hope you don't mind. I'm a beginner to Java in general.
I am using the JDK 8 for Java SE, targeting for desktop platforms. For Java, I understand that there's several ways of deploying an application, to name a few: 1) as an Applet, that runs inside a web browser, 2) Web Start (JNLP)? which I believe can be run inside a browser or desktop but connects to a server for updates and the like, 3) and as a JAR, which is simply an archive of the application files. For now I only care about deploying as a JAR. I've read the documentation on Oracle for using the command-line utilities for creating a JAR by specifying a Manifest file and the class files, which I was able to get working for a "Hello World" application. Now the problem is how 3rd party libraries come into the picture. I know with asm/C/C++ you simply statically link the libraries or provide DLL's for them. How is this usually handled with Java? First, I followed the tutorial here: http://www3.ntu.edu.sg/home/ehchua/programming/opengl/JOGL2.0.html I downloaded "jogamp-all-platforms" into C:\, then modified my CLASSPATH environment variable into: ".;C:\jogamp-all-platforms\jar\jogl-all.jar;C:\jogamp-all-platforms\jar\gluegen-rt.jar". Then I compiled one of the sample programs in the tutorial into "Test.class", then simply running "java Test" works perfectly. The problem comes with trying to JAR this program. "java Test" works, but "jar cfm Test.jar Manifest.txt Test.class" "java -jar Tech.jar" comes up with "Error: Could not find or load main class Test" I tried manually specifying CLASSPATH as a command line parameter, but it doesn't work. I know this is not a problem with JAR itself, because I was able to get JAR working for a Hello World application. So the problem is obviously with JAR'ing with a third party library. Any advice on this issue? |
I guess this is a typo? You create a jar called Test but try to run a jar called Tech. This should explain most of what you need: http://docs.oracle.com/javase/tutorial/deployment/jar/basicsindex.html |
Whoops, yes that is a typo. I typed it correctly in the command-line prompt.
I have read that tutorial, but I still get the error. |
Administrator
|
In reply to this post by cinolt
Depends on what form the third-party library is provided in. If the library is a JAR, you can: 1. Include that JAR with your app distribution, side-by-side with your own as a separate file. 2. Unpack that JAR and repack its contents into your own (may violate the license of the third-party library) 3. Pack that JAR inside your JAR (requires special treatment to load its contents at runtime) If the third-party library is a dll/so/dylib, you can: 1. Include them in your app distribution, side-by-side with your files. When you call loadLibrary() you just give it the path to your own program's directory. 2. Pack them into your JAR, then unpack them into a temp directory when your program runs (this is what JOGL does). |
The library is in JAR. I'll include the JAR with my app distribution. Why does the error in OP occur?
|
Administrator
|
Probably you need to add your library JAR to the -classpath or inside your executable JAR's manifest file. If you Google around you can find lots of help on this sort of thing, e.g. http://javarevisited.blogspot.com/2012/10/5-ways-to-add-multiple-jar-to-classpath-java.html.
|
The library JAR's are in the CLASSPATH. The error occurs. Even when specified in -cp, even when specified in the global environment variable.
|
When you use java -jar all classes must be in the JAR or pointed to by the Class-Path attribute of the MANIFEST.MF file. Environment variables and -cp arguments are ignored (http://docs.oracle.com/javase/7/docs/technotes/tools/windows/java.html).
Here is how to set up the manifest.mf file http://docs.oracle.com/javase/tutorial/deployment/jar/downman.html But usually the "can not find main class" usually means exactly that it can not find the main class. Otherwise you would get classloader errors or other errors. Make a simple hello-world java class without external libraries and see if you can get it running from a jar-file, when that works go on to add external libraries and see what happens. |
Free forum by Nabble | Edit this page |