java jar 类路径错误帮助
因此,我在文件夹 C:\folder\folderbaby 中设置了一个可执行 jar,
该 jar 包含使用 jdbc 对 mysql 的一大堆调用
如果我从命令行进入 C:\folder\folderbaby,然后运行 java,则 - jar thejar.jar,它会正常运行并访问数据库,
但是如果我 cd 到 C:\ 然后运行 java -jar C:\folder\folderbaby\thejar.jar,它将返回一大堆错误:
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
首先,jar 需要一个 .prop 文件,其中包含有关要访问的数据库的信息。 prop 文件包含在 jar 本身内部...我猜错误是存在的,因为即使 prop 文件位于 jar 内部,它也找不到 prop 文件,
我该如何解决这个问题,以便即使 jar 可以调用来自它所在目录之外的目录?
##################编辑: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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我相信问题在于 Manifest 行
Rsrc-Class-Path
是 Eclipse 扩展(它不是 Oracle 的 Jar 文件规范的一部分),并且需要在运行时解析的 jar 文件而是在Class-Path
行上。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 theClass-Path
line instead.您应该使用
这将设置类路径,然后运行您请求的 jar。默认情况下,java 使用当前目录作为类路径,当您移动到不同的文件夹时,它无法看到 jar 所在的位置。
You should use
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.