测试串行 SQLite JDBC 时出现 NoClassDefFoundError 错误
我无法运行 SQLite 示例,这令人抓狂。我收到一条复杂的多行错误消息,包括:
Exception in thread "main" java.lang.NoClassDefFoundError: Sample/jar
...
Could not find the main class: Sample.jar.
我怀疑可能涉及类路径错误,因此这是我的类路径(Win7):
.;C:\Program Files\Java\jdk1.6.0_25\lib;C:\ Program Files\Java\jre6\lib\;E:\Dropbox\Personal\Development\BlueJ 示例;C:\Program Files\Java\jdk1.6.0_25\lib\sqlite-jdb-3.7.2.jar
我正在 E:...BlueJ 示例文件夹中运行命令提示符(FWIW 正常的 "java -jar hello.jar "
命令提示符在此文件夹中运行得很好)。有人可以帮我弄清楚我做错了什么吗?
I am unable to run the SQLite sample and it's maddening. I get a complex, multi-line error message including:
Exception in thread "main" java.lang.NoClassDefFoundError: Sample/jar
...
Could not find the main class: Sample.jar.
I suspect a classpath error might be involved, so here is my classpath (Win7):
.;C:\Program Files\Java\jdk1.6.0_25\lib;C:\Program Files\Java\jre6\lib\;E:\Dropbox\Personal\Development\BlueJ examples;C:\Program Files\Java\jdk1.6.0_25\lib\sqlite-jdb-3.7.2.jar
I am running the command prompt inside the E:...BlueJ examples folder (FWIW a normal "java -jar hello.jar"
command prompt runs just fine from within this folder). Can somebody pls help figure out what am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的类路径需要包含对 jar 文件本身的引用,而不仅仅是它所在的目录。
Your classpath needs to include a reference to the jar file itself, not just the directory it is in.
抱歉,不得不开始另一个答案,因为这个答案相当冗长。
很高兴一切顺利,但我认为您可能误解了我在之前的回答中想说的内容。
类路径是 java 将查找类的一系列位置。如果类路径元素是一个文件夹,那么其中的所有 .class 文件都将包含在搜索中。 JAR 文件本质上是压缩文件夹,因此您必须在类路径中包含 JAR 文件的名称。您可以将实际的 jar 文件放在您喜欢的任何目录中,只要它在类路径上引用即可(使用文件的完整路径)。在您对我的其他答案的评论中,您已将 sqlite-jdbc.jar 添加到类路径文件中,但由于您没有使用完整文件路径引用它,因此 java 在运行时只会在当前目录中查找。
希望这是有道理的 - 并感谢您的接受:)
Sorry, had to start another answer as this one is quite longwinded.
Glad things are working but I think you probably misunderstood what I was trying to say in my previous answer.
The classpath is a series of places that java will look for your classes. If the classpath element is a folder then all .class files in there will be included in the search. Jar files are essentially compressed folders, so you have to include the name of the jar file on the classpath. You can put the actual jar file in any directory you like so long as it is referenced on the classpath (using the full path to the file). In your comment to my other answer you have added the sqlite-jdbc.jar to the classpath file but since you have not reference it with a full file path java will only look in the current directory when you run.
Hope that makes sense - and thanks for the acceptance :)