I'm having issues trying to use the Connector/J JDBC driver in my code.
The code I have written uses the Class.forName("com.mysql.jdbc.Driver").newInstance() to load the class before DriverManager.getConnection() is used to load the driver.
This results in the ClassNotFoundException for com.mydql.jdbc.Driver. I have the binary JAR file for the mysql connector, mysql-connector-java-5.1.26-bin.jar.
My code is packaged into a JAR file by building in Netbeans.
To run the code I am using the following
java -classpath "/path/to/mysql-connector-java-5.1.26-bin.jar" -jar MyJarFile.jar
This gives the exception: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
Adding System.out.println(System.getProperty("java.class.path")); to the beginning of the program prints only MyJarFile.jar.
I have tried adding the jar file to the CLASSPATH variable using export, and setting the last part of the -classpath flag to lib/*, but with the same results.
I tried running the program from a .class file, instead. It complained about being unable to find or load the main class. It would run only when both the wildcard was used in the classpath and the MyJarFile.jar was in that location. It would simply hang at the loading of the Driver, though.
Does anyone have any thoughts as to what is going on?
Try don't mix -cp and -jar options, this might work:
java -cp "mysql-connector-java-5.1.26-bin.jar:MyJarFile.jar" my.package.Main
for *nix or
java -cp "mysql-connector-java-5.1.26-bin.jar;MyJarFile.jar" my.package.Main
for windows
where my.package.Main is your main class.
Related
I am trying to execute a java application, which is packaged as jar archive.
java -cp .\lib\json.jar -jar ".\myarchive.jar"
I get an error saying that the classes inside my json.jar archive cannot be found.
Exception in thread "main" java.lang.NoClassDefFoundError: json/serializers/JsonTypeResolversInstance
Caused by: java.lang.ClassNotFoundException: json.serializers.JsonTypeResolversInstance
at java.net.URLClassLoader.findClass(URLClassLoader.java:387)
at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:352)
at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
... 2 more
The jar file contains the class, so i think this error should not happen. When executing the code using my IDE it runs without errors.
I have tried to fix this in many ways, without success.
Using the -cp and -jar options at the same time does not work. When you use the -jar option, then the -cp option is ignored.
There are two ways to run code in a JAR file in Java.
First way: Use the -cp option and specify the name of the class that contains the main method on the command line. For example:
java -cp .\lib\json.jar;.\myarchive.jar com.mypackage.MyMainClass
When you do it like this, you specify the classpath on the command line (using -cp). The classpath must contain all JAR files and/or directories that contain all the classes that the application needs. You must also specify the name of the class to run on the command line.
Also, when you do it like this, the manifest file that might be present in the JAR file is ignored.
Second way: Use the -jar option. For example:
java -jar .\myarchive.jar
When you do it like this, then Java will look at the manifest file in the JAR file. The classpath and the name of the class to run will be taken from the manifest file, and the -cp option on the command line will be ignored.
For details see the documentation of the java command.
Still get compile error ClassNotFoundException from com.mysql.cj.jdbc.Driver
from java-8 mysql-connector-java-5.1.147.jar and mysql-connector-java-5.1.47-bin.jar (though most postings say it is the bin.jar that is needed)
I unpacked the tar file and copied the two mysql-connector jar files to /usr/lib/jvm/java-8-oracle/lib and to my execution folder /var/www/html for overkill
Also, I put classpath in /etc/environment, and in java file and when compiling (tried each separately first).
then compiled, tried with and without in compile
Get the same error in all these cases (and other permutations):
Exception in thread "main" java.lang.ClassNotFoundException:
com.mysql.cj.jdbc.Driver
...
initially called from bbfq.main
Any other ideas of where I'm not coding the classpath correctly?
(this posting may not show the spacing and indents of code as I wrote it)
Thanks.
In /etc/environment:
JAVA_HOME="/usr/lib/jvm/java-8-oracle/bin"
export JAVA_HOME
CLASSPATH=".:/usr/lib/jvm/java-8-oracle/lib:/var/www/html"
export CLASSPATH
in java file:
import java.sql.*;
import java.io.*;
public class bbfq {
....
Connection con=null;
...
try {
Class.forName("com.mysql.cj.jdbc.Driver");
con=DriverManager.getConnection("jdbc:mysql//localhost:3306/drupal7" "id" "password");
....
Manifest file bbfq.mf:
Manifest-Version: 1.0
Class-Path: phidget22.jar mysql-connector-java-5.1.47.jar mysql-connector-java-5.1.47-bin.jar
compiled with and without declaring classpath:
sudo javac -classpath bbfq.java
or
sudo javac -classpath .:mysql-connector-java-5.1.47.jar:mysql-connector-java-5.1.47-bin.jar bbfq.java
and tried to run with or without classpath:
sudo java -jar bbfq.jar
or
sudo java -classpath .:bbfq:mysql-connector-java-5.1.47-bin.jar -jar bbfq.jar
Thanks.
First, remove Class.forName("com.mysql.ch.jdbc.Driver");. That has not been necessary for many years, because JDBC drivers are located using the service provider process.
Even if it were warranted, your Class.forName call assumes the driver class is com.mysql.ch.jdbc.Driver. That is no longer the case, and in fact there is no such class in the driver .jar file, so the Class.forName call will always fail.
Second, the -classpath option is ignored when using the -jar option. From the documentation:
When you use -jar, the specified JAR file is the source of all user classes, and other class path settings are ignored.
When you use the -jar option, only the manifest’s Class-Path attribute is used to add other files to the classpath.
The Class-Path attribute is actually a list of relative URLs, separated by spaces. They are relative to the .jar file containing the manifest.
So, you must place mysql-connector-java-5.1.47-bin.jar in the same directory as the bbfq.jar for it to be picked up by the classloader when it reads the Class-Path manifest attribute.
I want to set a cron job for my java code and I have tried this from project directory
java -classpath .:/home/project/lib/* pkg_name.my_code
and it works fine, however I dont know how to run it from any other directory[like in script]
I have tried to add diroctry (having compiled classes) in classpath like this
java -classpath .:/home/project/lib/*;/home/project/pkg_name/* pkg_name.my_code
also tried
java -classpath ".:/home/project/lib/*;/home/project/pkg_name/*" pkg_name.my_code
but it gives error:
**Error: Could not find or load main class pkg_name.my_code
**
can any please help me ?
If you want to run your project from another directory, then you need to include your project in classpath. So you can do this
java -classpath ".:/home/project/lib/*:/home/project" pkg_name.my_code
For example :
java -classpath ".:/home/test/runjavafromanotherdirectory/lib/*:./runjavafromanotherdirectory" com.test.Main
One of your mistake is you are using ; instead of :.
I've been reading up on this error but am now confused. I'm trying to run my java test file from the win7 cmd line but am getting that dreaded error cannot find class in classpath.
The script runs fine as a testNG file inside the Eclipse IDE but not from the cmd line.
My classpath dump:
C:\lib\selenium-java-2.37.0.jar;
C:\lib\selenium-server-standalone-2.37.0.jar;
C:\EclipseIDE\ProteinBar\bin;
C:\EclipseIDE\ProteinBar\src\com\proteinbar\staging;
C:\TestNG;
My cmd line test string:
java -cp C:\lib\*;C:\EclipseIDE\ProteinBar\bin org.testng.TestNG DeleteBillingAddress.xml
Thanks for any help...
it doesn't appear that the testng jars are on your classpath. I'm guessing they live under c:/testng so you'll want to add something to your -cp reflecting that.
You need to specify the jars individually, e.g.,
java -cp C:\lib\selenium-java-2.37.0.jar;C:\lib\selenium-server-standalone-2.37.0.jar;...
I have Mysql installed on my Linux box and wrote a sample program to access one of it's table.
I am using 'mysql-connector-java-5.1.10.jar'
The code is working fine if i put the jar in 'jre/lib/ext'. However, other ways of recognizing that jar are not working. I tried with setting $CLASSPATH and tried to use '.' current directory.
It's failing with the following error :
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306
at java.sql.DriverManager.getConnection(DriverManager.java:602)
at java.sql.DriverManager.getConnection(DriverManager.java:185)
I usually don't use the global $CLASSPATH variable, the easiest way to get it running is
java -cp .;/path/to/mysql-connector-java-5.1.10.jar[;<other libs>] pkg.name.MyApplication
Sidenote
If you have your application exportet to a jar with a Main-Class attribute ("executable jar") and start it with java -jar myjar.jar, then you have to add all required libraries to the jars manifest, $CLASSPATH and -cp are ignored in this case. And that's why I usually don't use the -jar option...
Edit
To answer your additional question: If the current directory was added to the classpath by default, then the location from where the application was started could influence the application itself.
Imagine an application inside a jar and a start command
java -cp application.jar com.example.Main
Now we have a defined environment: only the content of application.jar (and the jre classes) are on the classpath and part of the application. If the current directory was added to the classpath automatically then all files at the current location (and at locations of all subfolders) would be on the classpath too, intend or not. With a result, that the application might work if started from the users home directory but maybe not if started from the root directory (/).