0

I stumbled upon a weird error while using JDBC sqlite with org.sqlite.JDBC my code compiles and runs fine on Windows. But when I tried moving it to Ubuntu it started showing this:

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.

I have no idea what is going on. Pls help.

Here is what happens inside my dist directory:

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
3
  • 1
    The classpath is ignored when you use -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 Commented Jun 9, 2014 at 0:43
  • After trying about a dozen combinations finally this worked java -classpath "sqlite-jdbc-3.8.4.3-SNAPSHOT.jar:Mall.jar" Mall.main YAY! Thanks! (the main class was inside Mall.java) - if it was the answer I would mark it as solved :) Commented Jun 9, 2014 at 9:39
  • damn... I lost it again... :( I'm in dist directory... issuing java -classpath "sqlite-jdbc-3.8.4.3-SNAPSHOT.jar:Mall.jar" Mall.main gives me Error: Could not find or load main class Mall.main. all files are there, my main class comes from Mall.java -> Mall.jar Commented Jun 9, 2014 at 9:54

2 Answers 2

2

The classpath is ignored when you use -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.

Error: Could not find or load main class Mall.main. all files are there, my main class comes from Mall.java and is in mall package which compiles to Mall.jar

So the correct command line is:

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

OP findings

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

it showed

mall/Mall.class

so I should have used mall.Mall as the class to run (instead of pulling my hair)

Sign up to request clarification or add additional context in comments.

Comments

0

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.

It proved to be more portable than Java in my case.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.