java jar classpath error help
-
25-10-2019 - |
문제
so I set up an executable jar that is in folder C:\folder\folderbaby
the jar contains a whole bunch of calls to mysql using jdbc
if I go from the command line and cd to C:\folder\folderbaby and then run java -jar thejar.jar, it would run properly and access the database just fine
but then if I cd to C:\ and then run java -jar C:\folder\folderbaby\thejar.jar, it would return a whole bunch of errors:
Trying to add database driver (JDBC): RmiJdbc.RJDriver - Error, not in CLASSPATH
?
Trying to add database driver (JDBC): jdbc.idbDriver - Error, not in CLASSPATH?
Trying to add database driver (JDBC): com.mckoi.JDBCDriver - Error, not in CLASS
PATH?
Trying to add database driver (JDBC): org.hsqldb.jdbcDriver - Error, not in CLAS
SPATH?
java.sql.SQLException: No suitable driver found for jdbc:idb=experiments.prp
java.lang.IllegalStateException: Not connected, please connect first!
false
First of all the jar requires a .prop file that contains information about the database to access. The prop file is contained inside the jar itself...I guess the error is there because it couldn't find the prop file even though the prop file is inside the jar
how would I go about fixing this so that the jar is callable even from directory outside where it is contained?
##################EDIT: Content of Manifest.MF#########################
Manifest-Version: 1.0
Rsrc-Class-Path: ./ mysql-connector-java-5.1.17-bin.jar weka-src.jar weka.jar j.jar
Class-Path: .
Rsrc-Main-Class: Main
Main-Class: org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader
해결책
I believe the problem is that the Manifest line Rsrc-Class-Path
is an Eclipse extension (it's not part of the Jar file spec from Oracle), and the jar files to be resolved at runtime need to be on the Class-Path
line instead.
다른 팁
You should use
java -classpath C:\folder\folderbaby -jar C:\folder\folderbaby\thejar.jar
This will set the class path, and then run the jar you requested. By default java uses the current directory as the classpath, and when you move to a different folder it can not see where the jars are located.