简体   繁体   English

Java SQLite org.sqlite.JDBC类路径损坏了吗?

[英]Java SQLite org.sqlite.JDBC classpath broken?

I stumbled upon a weird error while using JDBC sqlite with org.sqlite.JDBC my code compiles and runs fine on Windows. 在将JDBC sqlite与org.sqlite.JDBC使用时,我偶然发现了一个奇怪的错误,我的代码可以在Windows上编译并正常运行。 But when I tried moving it to Ubuntu it started showing this: 但是,当我尝试将其移至Ubuntu时,它开始显示以下内容:

Exception in thread "main" java.lang.ClassNotFoundException: org.sqlite.JDBC
        at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
        at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Class.java:259)
        at mall.SQLiteJDBC.<init>(SQLiteJDBC.java:27)
        at mall.AllegroReader.<init>(AllegroReader.java:33)
        at mall.Mall.main(Mall.java:31)

I'm running it with java -classpath "sqlite-jdbc-3.7.2.jar" -jar Mall.jar" and java -classpath "sqlite-jdbc4-3.8.2-SNAPSHOT.jar" -jar Mall.jar with both versions in the same directory as my jar and I've tried a dozen different options specifying classpath and it behaves exactly the same. I tried openjdk and oracle jdk. I tried rebuilding it on Ubuntu, changing ant .xmls, changing paths, etc. 我正在使用java -classpath "sqlite-jdbc-3.7.2.jar" -jar Mall.jar"java -classpath "sqlite-jdbc4-3.8.2-SNAPSHOT.jar" -jar Mall.jar一起运行版本与我的jar相同的目录中,我尝试了十二种不同的选项来指定类路径,并且其行为完全相同。我尝试了openjdk和oracle jdk。我尝试在Ubuntu上重建它,更改ant .xmls,更改路径等。

I have no idea what is going on. 我不知道发生了什么。 Pls help. 请帮助。

Here is what happens inside my dist directory: 这是我的dist目录内发生的情况:

work1@workwork:/var/www/mall/dist$ ls
mall.db  Mall.jar  Mall.jar.old  sqlite-jdbc-3.8.4.3-SNAPSHOT.jar

work1@workwork:/var/www/mall/dist$ java -classpath "sqlite-jdbc-3.8.4.3-SNAPSHOT.jar:Mall.jar" Mall
Error: Could not find or load main class Mall

The classpath is ignored when you use -jar . 当使用-jar时,将忽略类路径。

You have to either include the dependencies in the jar (or at least have the jar manifest point to them), or run it with -classpath sqlite.jar:Mall.jar the.main.class . 您必须在jar中包含依赖项(或至少使jar清单指向它们),或使用-classpath sqlite.jar:Mall.jar the.main.class运行它。

Error: Could not find or load main class Mall.main. 错误:找不到或加载主类Mall.main。 all files are there, my main class comes from Mall.java and is in mall package which compiles to Mall.jar 所有文件都在那里,我的主类来自Mall.java ,并且在mall包中,可编译为Mall.jar

So the correct command line is: 因此正确的命令行是:

java -classpath "sqlite-jdbc-3.8.4.3-SNAPSHOT.jar:Mall.jar" mall.Mall

OP findings OP发现

to view the classes in jar use jar tf Mall.jar - from this I got mall/Mall.class meaning my class containing main was mall.Mall 要查看jar中的类,请使用jar tf Mall.jar从这里我得到mall/Mall.class这意味着我包含main的类是mall.Mall

it showed 这显示了

mall/Mall.class 商场/购物中心

so I should have used mall.Mall as the class to run (instead of pulling my hair) 所以我本应该使用Mall.Mall作为班级(而不是拉扯我的头发)

After spending over 6 hours total with many failed attempts at running "portable" jar package using classpath and whatnot, after having tried OneJar and jarjar to no avail (ended up with Class file too large! ) I decided to write the offending piece of code in PHP. 在总共花费了6多个小时,并多次尝试使用classpath和诸如此类的东西来运行“便携式” jar包失败之后,尝试了OneJar和jarjar都无效(最终导致Class file too large! )之后,我决定编写有问题的代码在PHP中。

It proved to be more portable than Java in my case. 在我看来,它比Java更可移植。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM