I am a pharmaceutical sciences student who is picking up coding as I go, so I apologize if the answer to this question has escaped me.
I wrote some java source code in NetBeans AND Eclipse. The code runs fine in both IDEs, however when I moved all the .java files to a UNIX environment I was successful at compiling the code, but the command line tells me it can’t find a class that is located in the same jar that I compiled with. I got warnings on compilations but I thought this would not affect the running of the code. I have searched and searched and can’t seem to find an answer. Here is my command line code (there are names of my programs, etc.):
[jknights@u2:~]$ cd chorus_jk
[jknights@u2:~/chorus_jk]$ ls
Chorus_JK.java EntropyNormal_JK.java Main_JK.java
colt.jar EstimateParzen_JK.java RA_reformatted_forCHORUS_JK.txt
Combination_JK.java LIST_JK.java
[jknights@u2:~/chorus_jk]$ javac -cp colt.jar ./*.java -Xlint:unchecked
.
. (I edited out the 100 warnings as they refer to unchecked items)
.
100 warnings
[jknights@u2:~/chorus_jk]$ jar cfe ChorusJK_RA.jar Main_JK ./*.class
[jknights@u2:~/chorus_jk]$ java -jar ChorusJK_RA.jar
89
317504
Exception in thread "main" java.lang.NoClassDefFoundError: cern/colt/matrix/DoubleMatrix2D
at Chorus_JK.init(Chorus_JK.java:24)
at Main_JK.main(Main_JK.java:23)
Caused by: java.lang.ClassNotFoundException: cern.colt.matrix.DoubleMatrix2D
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
... 2 more
the “89” and “317504” are summary outputs for the file that is read in so it appears that the compile was successful; however, when the program gets to the calculation part, it gives me the Exception in thread “main” java.lang.NoClassDefFoundError: cern/colt/matrix/DoubleMatrix2D error. Thanks so much for any help!
Your difficulty in getting your code to run from the command line demonstrates the value of an IDE. If I understand your work flow correctly, you developed your code on one machine with an IDE, then you had to run it on a different computer without a GUI environment, so you were stuck with the command line. This is fairly common, especially for web application development.
Netbeans creates a dist folder (distribution) and you can configure your project to copy your libraries to a lib folder (I think that’s the default behavior). The manifest file contained inside the jar file should contain a line like this:
Class-Path: lib/colt.jar
You can examine the contents of your jar file with a zip tool or within your IDE. In netbeans from the file view you can expand your dist folder and then expand the jar to examine its contents.
Before moving your code from a graphical environment to a command line, try just running your program without the IDE. You should be able to double-click the jar file in the /dist folder and it should run. If it does, then you should be able to move the entire /dist folder to a different computer with a different operating system and it should still run without needing to add -cp colt.jar to your command line arguments.
You should not need to re-compile your code to get it running on a different operating system. Simply move the /dist folder and you should experience the joy of “write once, run anywhere.”