last day (14 days later) » 

6:12 AM
0
A: Unable load jar from my jar: NoClassDefFoundError

Multisync Finding a JAR file using a classpath is similar to finding a package file in a classpath. The difference is that when you specify a path for a JAR file, you must include the name of the JAR file at the end of the path. So you can try something like this: java -classpath /usr/java/jdk1...

 
Thanks, you means run this first java -classpath /usr/java/jdk1.6.0_21/lib/javax.mail.jar:/usr/java/jdk1.6.0_21/jre/lib/postgresq‌​‌​l-9.1-901.jdbc4.jar -jar weekly.jar then it will load those jar files, then run java -jar weekly.jar ?
 
@Jesse Siu No, "-classpath" defines additional entries in your classpath for the current execution. You just need to run one command (with your paths to the jars)
 
I try the command but it still java.lang.ClassNotFoundException: org.postgresql.Driver seems the jdbc.jar not loaded yet?
 
@Jesse Siu Yes. Check the path to your jar in "-classpath" option + check that the class, which is not found, is in the jar
 
Thanks, I checked the classpath for jdbc.jar, it's right and I used it in my Mac it also works. but in the CentOS server it still shows java.lang.ClassNotFoundException: org.postgresql.Driver
 
6:12 AM
@Jesse Siu I can't find "postgresql-9.1-901" here:jdbc.postgresql.org/download.html Are you sure you're using the correct jar?
hi
 
Thanks, Yes this jar is quite old,but it still works. I can running it in exclipse. And I also try to download and use postgresql-9.3-1102, it also shows same error
 
at first you had problem with mail
you added -classpath to you command line
now you have problem with another class
am I right?
 
yes, seems it find the javaxmail class
 
ok
which command line are you using
 
java -classpath /root/java/javax.mail.jar:/root/java/postgresql-9.3-1102.jdbc4.jar -jar weekly.jar
 
6:17 AM
strange
try to change : to ;
 
it will shows command error
 
and if you switch these two locations?
java -classpath /root/java/postgresql-9.3-1102.jdbc4.jar:/root/java/javax.mail.jar -jar weekly.jar
 
java.lang.ClassNotFoundException: org.postgresql.Driver
seems the jdbc can't load
 
But the class exists in the jar
if it works on other platform than maybe it's some naming issues
try to rename your jar to jdbc.jar for example
 
java -classpath /root/java/postgresql-9.1-901.jdbc4.jar:/root/java/javax.mail.jar -jar report.jar I changed to report.jar
same problem. So sad
 
6:28 AM
What's the whole stacktrace
 
you means all files in the jar?
java.lang.ClassNotFoundException: org.postgresql.Driver
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
 
try to put postgresql-9.1-901.jdbc4.jar into jre/lib/ext
 
it works
finally
why just the jdbc.jar need put into /ext
 
ok, so I think the problem is the order in which classes are loaded
when you put the jar in /ext it is loaded earlier
when you use -classpath with this jar it is loaded later than it's required by other classes
 
OK, Thanks again
 
6:37 AM
You're welcome
P.S. it's not the order of classloading
 
OK, Thanks
 
you don't have imports of org.postgre... in your code. You're using classForName
that's why it is not loaded using "-classpath"
so you need to put it somewhere in common place
 
okok, i try to add import org.postgre in my code. :-)
 
then it will be difficult to switch to another database
it was done for purpose - you have a driver of a database in some common place
but your code doesn't contain anything related to the specific database
so you can switch to another db just by changing jar in the common place
please accept my answer if it was helpful
 
thanks, no problem usually i running java in eclipse, it is first time i running it in linux, thanks for you help
 

last day (14 days later) »