CLASSPATH issue while accessing Mysql on Linux - java

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 (/).

Related

Customize Spring Boot executable-jar startup script

I'm trying to run Spring Boot executable-jar built using spring-boot-maven-plugin on a Linux machine. The machine has multiple jdks installed, the one on PATH is jdk8 and changing it is unfortunately not an option. My executable-jar however needs jdk17, so when I just launch it as is I get UnsupportedClassVersionError.
I was following the official documentation and created the corresponding .conf file to override JAVA_HOME. But this does not seem to solve the issue:
[root#ios-maket updater-new]# ls
updater-new-3.0-SNAPSHOT.conf updater-new-3.0-SNAPSHOT.jar
[root#ios-maket updater-new]# cat updater-new-3.0-SNAPSHOT.conf
JAVA_HOME=/opt/jdk-17/bin/java
[root#ios-maket updater-new]# ./updater-new-3.0-SNAPSHOT.jar
Application is running as root (UID 0). This is considered insecure.
Exception in thread "main" java.lang.UnsupportedClassVersionError...
On the other hand if I run it manually everything works fine:
[root#ios-maket updater-new]# /opt/jdk-17/bin/java -jar ./updater-new-3.0-SNAPSHOT.jar
[main] INFO com.icl.ios.fias.updaternew.UpdaterNew - Starting UpdaterNew using Java 17.0.6
What am I doing wrong?
Setting JAVA_HOME is not enough, you also need to set PATH to point to JAVA_HOME/bin.
JAVA_HOME=/opt/jdk-17
PATH=${JAVA_HOME}/bin:$PATH
java -jar updater-new-3.0-SNAPSHOT.jar
Try running the jar with -Dloader.path to specify the config manually.
java -Dloader.path=./updater-new-3.0-SNAPSHOT.conf -jar ./updater-new-3.0-SNAPSHOT.jar
If this still does not work, then probably there is an issue with your config file, but from what I can see, your config file looks okay, unless the java path is incorrect.

How to run JMeter through CMD from jdk/bin directory?

I want to run JMeter test plans from a UNIX server where env variables for java is not set. Its a test server and I dont have access to set that. We have different JDK versions and all are sitting in directories. I need to run my JMeter TestPlans on this server. I went to JDK /bin folder and tired to execute the below command
$ {jmeter-path}/bin/jmeter -nt testplan.jmx -l testresult.jtl
but this says
./bin/java: not found
But if I do simple java -version it shows the version result.Is that something that JMeter needs specifically the java env variable set or it wont run ? I dont have permission to set and I want to run the testplan using the JDK/JRE from its directories. A help would be appreciated. Thanks in advance!
UPDATE:
I think I could work around this by editing the jmeter script file as suggested in the one of the comments. Since my requirement was running the JMeter from a specific server, I could achieve this by editing the JAVA_HOME variable value in the script.
JMeter looks for java executable in system PATH so you have 2 options:
Add bin folder of your JDK or JRE to PATH, something like:
PATH=$PATH:/location/of/your/jbk/bin && export PATH && {jmeter-path}/bin/jmeter -nt testplan.jmx -l testresult.jtl
Or if you have java in PATH just run ApacheJMeter.jar like:
java -jar {jmeter-path}/bin/ApacheJMeter.jar
You might also want to use jmeter.sh wrapper script instead of jmeter, it has some logic regarding java binary location
More information: Get Started With JMeter: Installation & Tests
The official JMeter Getting Started documentation says this:
To install a release build, simply unzip the zip/tar file into the directory where you want JMeter to be installed. Provided that you have a JRE/JDK correctly installed and the JAVA_HOME environment variable set, there is nothing more for you to do.
Based on the symptoms that you reported, I think that you have not set JAVA_HOME correctly. It should be set to an absolute path to your Java installation's top directory.

Java mysql-connector ClassNotFoundException despite filing posts

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.

classpath missing jar file even though specified

I've read a number of answers on classpaths, and i can't understand why this isn't working; Notice the directory listing beforehand that shows jdbc.jar is clearly in the Drivers directory, and then the java command still fails. Also, on my windows machine I can run this exact program with the same setup if i put a ; at the end of the -cp ( "./Drivers/*; ). java -version is java version "1.6.0_37".
[~/test ] $ ls -1 ./Drivers/
dbdump-0.1.0-SNAPSHOT-standalone.jar
jt400.jar
jtds.jar
[~/test ] $ java -cp "./Drivers/*" dbdump.core input.config
Exception in thread "main" java.sql.SQLException: No suitable driver found for jdbc:jtds:sqlserver://SERVER;instance=MSSQLSERVER;database=dbone;
at java.sql.DriverManager.getConnection(DriverManager.java:602)
at java.sql.DriverManager.getConnection(DriverManager.java:154)
at clojure.java.jdbc$get_connection.invoke(jdbc.clj:219)
at dbdump.core$_main.invoke(core.clj:60)
at clojure.lang.AFn.applyToHelper(AFn.java:161)
at clojure.lang.AFn.applyTo(AFn.java:151)
at dbdump.core.main(Unknown Source)
[~/test ] $
java handles path same whether be it on windows or linux.
So question is if ; is working on windows why it will not work on linux.
Secondly have you tried executing the same code or program on any other linux machine.
I am presuming may be the jar is corrupt. What about pin-pointedly mentioning the jar. not just * to pick up all jars
yep what nishant mentions is the correct usage of java call. along with loading the jar we also need to call the class
"lib/*" should be enough (reference):
java -cp "Drivers/*" com.example.Classname
Class path entries can contain the basename wildcard character *,
which is considered equivalent to specifying a list of all the files
in the directory with the extension .jar or .JAR. For example, the
class path entry foo/* specifies all JAR files in the directory named
foo.
For what it is worth, this is how things are handled on windows.

Classpath contains only the running file

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.

Categories