ClassNotFoundException when connecting to Mysql with JDBC - java

I'm getting the following error when I try to run a simple Java JDBC program at the command line:
Exception in thread "main" java.lang.NoClassDefFoundError: LoadDriver/java
Caused by: java.lang.ClassNotFoundException: LoadDriver.java
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:315)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:330)
at java.lang.ClassLoader.loadClass(ClassLoader.java:250)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:398)
Here's the simple Java program, copied right out of the JDBC docs:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
// Notice, do not import com.mysql.jdbc.*
// or you will have problems!
public class LoadDriver {
public static void main(String[] args) {
try {
// The newInstance() call is a work around for some
// broken Java implementations
Class.forName("com.mysql.jdbc.Driver").newInstance();
} catch (Exception ex) {
throw ex;
// handle the error
}
}
}
Problem is, I'm bloody sure my bash shell $ClASSPATH variable is pointed at the correct .jar file. To be sure, I copied the JDBC .jar to the same directory as my program and ran it as follows:
java -classpath ./mysql-connector-java-5.1.12-bin.jar LoadDriver.java
I still get the same error.
Edit:
I followed Powerlord's suggestion below, and now I am still getting virtually the same exception.
I entered:
javac -classpath ./mysql-connector-java-5.1.12-bin.jar LoadDriver.java
java LoadDriver
Whether or not I leave the classpath flag on the second command seems not to matter. I am still getting:
Exception in thread "main" java.lang.NoClassDefFoundError: LoadDriver
Caused by: java.lang.ClassNotFoundException: LoadDriver
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:315)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:330)
at java.lang.ClassLoader.loadClass(ClassLoader.java:250)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:398)

I think your syntax is just wrong here. Have you already compiled LoadDriver.java using:
javac -classpath ./mysql-connector-java-5.1.12-bin.jar LoadDriver.java ?
If so, then you should be able to do :
java -classpath ./mysql-connector-java-5.1.12-bin.jar LoadDriver
(Note that I removed the '.java' from the end)

Short version:
javac requires you to put the .java at the end, java requires you to not put the .java at the end.
As Jim Garrison noted before he deleted his answer, this command-line to run the program is wrong.
java -classpath ./mysql-connector-java-5.1.12-bin.jar LoadDriver.java
This tells Java to load LoadDriver/java.class
What you actually want is
java -classpath ./mysql-connector-java-5.1.12-bin.jar LoadDriver
Provided of course that you compile it first with
javac -classpath ./mysql-connector-java-5.1.12-bin.jar LoadDriver.java

The problem is not the missing driver, it's the missing LoadDriver class. You need to compile the .java source file to a .class file first:
javac LoadDriver.java

Related

How to compile a java program with imported classes?

I am trying to get started with the API of the java software weka. I wrote the following code for testing:
import weka.core.Instances;
import java.io.BufferedReader;
import java.io.FileReader;
public class hello_weka {
public static void main(String[] args) throws Exception {
BufferedReader reader = new BufferedReader(new FileReader("/home/aljoscha/Masterarbeit/weka_examples/iris.arff"));
Instances data = new Instances(reader);
reader.close();
// setting class attribute
data.setClassIndex(data.numAttributes() -1);
System.out.println(data);
System.exit(0);
}
}
It works fine when I execute it in Eclipse.
However I can't get it to run in the Terminal.
I tried to provide the .jar path during compilation and then execute the program from the directory of the compiled class.
javac -cp /usr/share/java/weka.jar hello_weka.java
java hello_weka
This approach does not work, I get the following error:
Exception in thread "main" java.lang.NoClassDefFoundError: weka/core/Instances
at hello_weka.main(hello_weka.java:8)
Caused by: java.lang.ClassNotFoundException: weka.core.Instances
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
... 1 more
What am I doing wrong?
I guess I am doing just some completely stupid stuff since I just start to code in Java. If so, please excuse me and try to tell me how I can do better.
Edit:
when I try the thing proposed in the answers I get the following Error:
Exception in thread "main" java.lang.NoClassDefFoundError: hello_weka
Caused by: java.lang.ClassNotFoundException: hello_weka
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
Could not find the main class: hello_weka. Program will exit.
Try this out:
java -cp /usr/share/java/weka.jar hello_weka
You need to provide the classpath to your JAR also when executing the program:
java -cp /usr/share/java/weka.jar hello_weka
You need to add also the current directory (where your own classes are stored) to the classpath:
java -cp .;/usr/share/java/weka.jar hello_weka
I finally found the answer: The answer of dunni is nearly correct. For me it works if I provide the classpath of both, the jar file and the classfile, but separated by a :.
java -cp /usr/share/java/weka.jar:/home/aldorado/myjavascripts/ hello_weka
Is it possible that this differs depending on the OS / JDK you are using?

Java reading an argument from terminal

I'm trying to implement the following:
public class Main {
public static void main(String[] args) {
//READ FILE IN
String filename = args[0];
System.out.println(filename);
}}
This compiles fine, but when I try to run java br/com/seimos/minijava/Main.java < a or java br/com/seimos/minijava/Main.java a for example, I get an error. Why?? (by the way, I need to get it so that I can do java br/xx/xx.../xx.
Thanks!
EDIT: Sorry, I typed it wrong initially. I DID run java not javac.
The error I get is:
Exception in thread "main" java.lang.NoClassDefFoundError: br/com/seimos/minijava/Main/java
Caused by: java.lang.ClassNotFoundException: br.com.seimos.minijava.Main.java
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
You don't pass arguments at compile-time, but at runtime.
javac --> compiles your Java program
java --> runs the generated bytecode
java br/com/seimos/minijava/Main.java -args
^ remove (.java)
Java runtime runs the .class bytecode-generated, which is of the name supplied, not the uncompiled .java source
Running a Java program is a two step process. First the .java file is compiled into .class files. Then you use the java command to execute the class files. Runtime arguments must obviously be passed at runtime when you invoke java.
For compiling, if the java file is with some package, you need to apply such as br.com.xxx.main.java. If that is just path to the java file that is ok. can you post the error?

Compile error with JOGL

I try to compile a simple Java program with JOGL OpenGL, from the command line. In the Eclipse all works fine, when I compile it from the command line with javac I get the following error:
Exception in thread "main" java.lang.NoClassDefFoundError: javax/media/opengl/GLEventListener
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(Unknown Source)
...
Caused by: java.lang.ClassNotFoundException: javax.media.opengl.GLEventListener
...
Could not find the main class: SimpleScene. Program will exit.
I already add to -classpath all the .jar files I find in JOGL directory. The Makefile to build the program is the following:
SimpleScene:
javac -Xlint:deprecation -classpath \
$(jogl)/joal.jar:\
$(jogl)/jogl.os.x11.jar:\
...
$(jogl)/nativewindow.awt.jar:\
$(jogl)/jogl_cg-natives-linux-i586.jar:. \
SimpleScene.java
I had a similar problem and figured out my simple mistake. GLEventListenere is an interface; not a class.
http://download.java.net/media/jogl/builds/archive/jsr-231-beta5/javadoc_public/javax/media/opengl/GLEventListener.html
I fixed my problem after realizing this because my code that uses GLEventListener should use the keyword 'implements' instead of 'extends'.

I am able to compile the java program but not able to run a java program

When I run the java program it gives following error:
Exception in thread "main" java.lang.NoClassDefFoundError: check
Caused by: java.lang.ClassNotFoundException: check
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
Could not find the main class: check. Program will exit.
The source code is:
import java.io.*;
class check {
public static void main (String [] args)
{
System.out.println("Hello");
}
}
~
~
You've got the CLASSPATH environment variable set, and it doesn't include . (dot), the current directory. Try this
java -cp . check
(That's java space dash cp space dot space check).
Please try by set the class path first then compile and execute the class Then your problem will be resolved.
For example at command prompt:
C:\> setclasspath=%classpath%;.;
C:\> javac check.java
C:\> java check
Now, you will get the output as Hello.

Manual setting CLASSPATH with -cp or -classpath does not work as expected

MyClassWithMainMethod.java uses classes of someJar.jar.
If I call:
java -cp someJar.jar MyClassWithMainMethod
I get the exception:
Exception in thread "main" java.lang.NoClassDefFoundError: MyClassWithMainMethod
Caused by: java.lang.ClassNotFoundException: MyClassWithMainMethod
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:315)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:330)
at java.lang.ClassLoader.loadClass(ClassLoader.java:250)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:398)
But when I set the CLASSPATH to my jar manually
export CLASSPATH=:/path/to/someJar.jar
it works calling
java MyClassWithMainMethod
What am I doing wrong?
What about
java -cp /path/to/someJar.jar MyClassWithMainMethod
If you don't give Java the complete path to the jar file, how do you expect that it would find it?
OK well the argument you give to "-cp" is the same sort of thing you use with the CLASSPATH variable - what happens when you do this:
java -cp .:someJar.jar MyClassWithMainMethod

Categories

Resources