Cannot load an SQLite JDBC Driver, Any Ideas? - java

So i need to write a program that accesses and modifies an SQLite DB for a school program and am looking at the basics firstly. Now, I am looking at this to get me started: link text. The problem is that when i compile Test.java (The example code on the website) and then run the command:
java -cp .:sqlitejdbc-v056.jar Test
like it tells me to, i get the following error:
Exception in thread "main" java.lang.NoClassDefFoundError: Test
Caused by: java.lang.ClassNotFoundException: Test
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
Could not find the main class: Test. Program will exit.
Test.java, Test.class and sqlitejdbc-v056.jar are all in the same folder so they should find each other. Does anybody have an idea about what i am doing wrong?

According to the java.lang.NoClassDefFoundError: Test, the Test class cannot be found in the CLASSPATH. But you wrote that Test.class is available in the current directory so I'm gonna guess something. Actually, I think that you are not using the right CLASSPATH separator character (which is platform dependent) and thus not building correctly the CLASSPATH. On Windows, you should use ; instead of :. If this happens to be the case, you should try this:
java -cp .;sqlitejdbc-v056.jar Test
If you are on a unix-like platform, forget this answer, the problem is elsewhere.

As the exception message is saying, the problem is that it cannot find the class Test. So the problem is not finding SQLite, but finding your Test class.
Just a guess, but I am wondering if there is an issue with how you compiled. In the Test.java, does the class belong to a package? If so, you need to compile it using the -d flag so that it gets put in the appropriate sub-directory.

Related

Unable to run Java code with Apache Commons

I've written a program to read in data from a text file and do some simple calculations, then print out those calculations. That part works great.
Afterward, I added in some code to do a t-test, using the TTest class (org.apache.commons.math3.stat.inference.TTest). So, I downloaded commons-math3-3.6.jar from the Apache Commons download page and put the JAR file in the same folder as the rest of my Java code for this program.
I use the following command in Windows to compile, which works fine:
javac -cp ./commons-math3-3.6.jar ./FootballTeam.java ./Main.java
But I can't figure out how to correctly run the program. I've tried this:
java Main
which executes everything up to the t-test perfectly, and then gives the expected error:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/math3/stat/inference/TTest
at Main.main(Main.java:32)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.math3.stat.inference.TTest
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 1 more
I've also tried this:
java -cp commons-math3-3.6.jar Main
which gives me this:
Error: Could not find or load main class Main
I cannot for the life of me figure out how to properly set the classpath here. Can someone provide me some assistance? Thank you!
The Main class cannot be found because the current directory (.) is not on your classpath.
To add it, on Windows:
java -cp ".;commons-math3-3.6.jar" Main
On *n?x:
java -cp ".:commons-math3-3.6.jar" Main

java.lang.NoClassDefFoundError with LWJGL

So, I am afraid that I have to join the crowds flocking here and asking: "What's wrong with my code?"
I've recently started coding in Java and I've had none of this error until I tried using lwjgl in a project. So, the question is: What is wrong with my JAR when I get the following:
java -cp \lib\win32\lwjgl.jar -jar Valor.jar
Exception in thread "main" java.lang.NoClassDefFoundError: org/lwjgl/opengl/Display
at bin.Disp.createWindow(Disp.java:18)
at bin.Thread.gameLoop(Thread.java:13)
at Main.main(Main.java:4)
Caused by: java.lang.ClassNotFoundException: org.lwjgl.opengl.Display
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 3 more
As is, I am extremely confused about this. I've gone through seven or eight posts here about this very problem, and another five or six on other web sites, yet none of the methods those people used seem to work for me.
Currently, my file structure is:
Valor
-Valor.jar
lib
win32
-lwjgl.jar
-lwjgl.dll
Manifest:
Manifest-Version: 1.0
Main-Class: Main
I did try defining a classpath here... Didn't work. So, I'm pretty much as confused as I can possibly get. Thank you for your help.
Edit: This error doesn't occur while running in Eclipse, only when I export and try to run it myself.
Try to run it like this:
java -cp lib\win32\lwjgl.jar;Valor.jar Main
The problem is that the -cp option is ignored when -jar is used.
Alternativelly you could extend the class path by adding a Class-Path: entry in your manifest and puting lib\win32\lwjgl.jar in it.
Class-Path: lib\win32\lwjgl.jar
use this link to install ljgl and define your classPath : http://www.lwjgl.org/wiki/index.php?title=Setting_Up_LWJGL_with_Eclipse
By running java -cp \lib\win32\lwjgl.jar -jar Valor.jar you're telling Java to look for LWJGL classpath near the root of your filesystem. Seeing your directory structure, use:
java -cp .\lib\win32\lwjgl.jar -jar Valor.jar
Notice the . at the beginning.

Issues understanding how to use Jython

I've just started learning Jython, and I'm having some issues with implementation. I've looked through the Demo files that come with Jython 2.5, and I'm still unsure how to implement Jython to get what I want.
Currently, I've got Java code executing a Python server as a Process. The Python server in turn starts a second section of Java code as a subprocess. I was originally going to replace the Python server with a Java NIO server, but that's given me no end of grief, and thus why I'm trying Jython. I also want to get all of this into a .jar.
I tried making a simple Python file (print "Hello World"). It runs with Jython, but when I try to run it using java (after doing jython -m compileall.) it says that it can't find main. I assume that I need to add something to my Python code to make it work, but I'm not sure what.
Edit: The exact error I'm getting is this-
Exception in thread "main" java.lang.NoClassDefFoundError: jythonTest
Caused by: java.lang.ClassNotFoundException: jythonTest
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: jythonTest. Program will exit.
I think the answer is CLASSPATH.
hello.py
print "Hello"
Let's run it
> python hello.py
Hello
> jython hello.py
Hello
Compile (I used py_compile to compile single file)
> jython -m py_compile hello.py
Run with java
> java -classpath d:\P\jython253\jython.jar;. hello$py
Hello
Note the dot in classpath. It is required for java to find your compiled class in current directory.

Able to compile but unable to run Java from cmd with jar files

I have a java file which uses jfreechart libraries, uses a text file from local drive and displays graph. Runs fine with eclipse. However, I want to run this from cmd prompt, other simple Java files are able to run successfully via cmd prmnt but not able to run this file.
PS: MyTool.java is able to compile without errors and class file is created, but not able to run.
1) This is how I am compiling it in cmd prompt: (gives 0 errors)
C:\Documents and Settings\hello.maga\workspace\MyTool\lib>javac -cp "gnujaxp.
jar;iText-2.1.5.jar;jcommon-1.0.16.jar;jfreechart-1.0.13.jar;jfreechart-1.0.13-e
xperimental.jar;jfreechart-1.0.13.jar;junit.jar;servlet.jar;swtgraphics2d.jar" MyTool.java
2) This is how I am running it:
C:\Documents and Settings\hello.maga\workspace\MyTool\lib>java -cp "gnujaxp.j
ar;iText-2.1.5.jar;jcommon-1.0.16.jar;jfreechart-1.0.13.jar;jfreechart-1.0.13-ex
perimental.jar;jfreechart-1.0.13.jar;junit.jar;servlet.jar;swtgraphics2d.jar" MyTool
Error for second command:
Exception in thread "main" java.lang.NoClassDefFoundError: MyTool
Caused by: java.lang.ClassNotFoundException: MyTool
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: MyTool. Program will exit.
What I don't understand is, if there are any errors, then it should not compile in first place, can someone educate me. Thank you very much.
You need to include "." in the classpath, like so:
java -cp ".;gnujaxp.jar;iText-2.1.5.jar;jcommon-1.0.16.jar;jfreechart-1.0.13.jar;jfreechart-1.0.13-experimental.jar;jfreechart-1.0.13.jar;junit.jar;servlet.jar;swtgraphics2d.jar"
From "Setting the class path": "The class path tells SDK tools and applications where to find third-party and user-defined classes -- that is, classes that are not Java extensions or part of the Java platform. The class path needs to find any classes you've compiled with the javac compiler -- its default is the current directory to conveniently enable those classes to be found."
However if you set the classpath yourself, the default no longer applies, and you're expecting it to load classes from the current directory. You'll have to add it manually, like by adding "." to the class path as Ed Staub recommended.
When compiling, your class wasn't needed on the class path, so to speak, since it's what was being compiled. You only needed all the other classes (in the jar files) on the class path for that. That's why you're able to compile but not run, even though you used an identical class path for both operations.

How to run a .class file in Windows 7 OS?

This is probably a stupid question, but how do I run a class file on windows 7? I usually create my own .java files and then use a basic IDE (with JDK6) to compile it to a class and run it automatically. My professor gave a .class file that we are supposed to play with extensively but I have no idea how to to run it here. Note that I cannot run it by typing:
java classname.class
because it contains gui stuff especially buttons and such. If I do the above I get the following error:
java.lang.NoClassDefFoundError: Test1/2/class
Caused by: java.lang.ClassNotFoundException: Test1.2.class
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
Could not find the main class: Test1.2.class. Program will exit.
Exception in thread "main"
Any help would be highly appreciated. Thanks.
When you run it, don't include the .class suffix:
java classname
In addition to the above, it should also make no odds what your O/S is. Java compiles to byte code, which is interpreted by your JVM. You clearly have one installed since you got the java error you have pasted above.
Like codeka said, you need to type java ClassName at the command line, and if the class has a main method, it will be run.
Java compiles each class that is defined in a particular source file into its own class (byte code) file. For example, Apple.class, Banana.class, and Cherry.class might get output after compiling Apple.java, if they are all defined there. So the actual name of the class in source will match the name of the file, minus the extension.
Now, let's say that someone accidentally (or intentionally, from the sound of it) renamed the class file. You have a file called WrongName.java, and you type java WrongName. Note that the output will begin with the line:
Exception in thread "main" java.lang.NoClassDefFoundError: WrongName (wrong name: RightName)
Where RightName is what it should be. At that point you would rename your file to RightName.class, type java RightName, and hopefully it will run. And if the name has a slash, then whatever precedes the slash is the name of the package. Let's say it's PackageName/RightName. First you need to create a directory called PackageName, and put RightName.class inside of it. Then, go one level up in your directories, and type java PackageName.RightName.
Note the different kinds of exceptions: ClassNotFoundException basically means that the class file was not found. NoClassDefFoundError means that the class definition was not found. If the class does not have a main method and you try to run it as a program, you will get NoSuchMethodError: main.

Categories