How to run java program in linux terminal using classpath - java

i am new to linux terminal and does not know how to run java file using classpath.
class Test{
public static void main(String[] args){
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException cnfe) {
System.err.println("Couldn't find driver class!");
}
}
}
The program compiled but unable to run.I have "mysql.jar" in the same directory. But i am unable to run this file. How can i run this file using classpath

Use:
java -cp ./:mysql.jar Test

Error is "ClassNotFound"?
You must include all the class files your application needs
java -classpath mysql.jar:. Test

Related

NoClassDefFoundError when the jar exists, specifically for mysql-connector-java-8.0.20.jar

I want to make sure I can connect to the MySQL server I have running locally with a simple Java class.
To test the connection I want to use the MysqlDataSource class that comes with the mysql-connector-java-8.0.20.jar
I'm on Ubuntu 18.04 LTS and have the jar located at:/usr/share/java/mysql-connector-java-8.0.20.jar.
Then I run the compiler command: $ javac -cp "/usr/share/java/mysql-connector-java-8.0.20.jar" App.java
Then: $ java App
And get the following output error:
Exception in thread "main" java.lang.NoClassDefFoundError: com/mysql/cj/jdbc/MysqlDataSource
at App.main(App.java:15)...
Output of $ mysql --version:
mysql Ver 14.14 Distrib 5.7.30, for Linux (x86_64) using EditLine wrapper
This is all my App.java file contains:
import java.sql.Connection;
import java.sql.SQLException;
import com.mysql.cj.jdbc.MysqlDataSource;
public class App {
public static void main(String[] args) {
MysqlDataSource dataSource = new MysqlDataSource();
dataSource.setUser("root");
dataSource.setPassword("test");
dataSource.setServerName("localhost");
Connection conn;
try {
conn = dataSource.getConnection();
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}```
Instead of java App, you must specify a classpath to include the jar at runtime. Like,
java -cp "/usr/share/java/mysql-connector-java-8.0.20.jar:." App

Having trouble running Java program with Command Prompt

I'm getting the error Error: Could not find or load main class ExcelFileEditor when trying to run my program. I compiled the program with no errors by:
javac -cp C:\Users\rperera\IdeaProjects\LinkingNames\libs\*;. C:\Users\rperera\IdeaProjects\LinkingNames\src\ExcelFileEditor.java
I tried doing:
java ExcelFileEditor
java -cp C:\Users\rperera\IdeaProjects\LinkingNames\libs\* ExcelFileEditor
but I keep getting the same error. I'd really appreciate it if someone could help me fix this problem!
public static void main(String[] args) {
//this allows the Py4J module in Python to use whichever methods it needs from this class
ExcelFileEditor editor = new ExcelFileEditor(new File(args[0]));
GatewayServer server = new GatewayServer(editor);
server.start();
}
The package is excel.writer
If you are not specifying where to place the generated class files then its going to be in the same directory as the source file.
Try
java -classpath C:\Users\rperera\IdeaProjects\LinkingNames\libs\*;C:\Users\rperera\IdeaProjects\Li‌​nkingNames\src excel.writer.ExcelFileEditor

Java SQLite3 Connection Failing

I am having some trouble connecting my java script to my SQLite3 database.
I have my directory holding my script as followed
C:/PROG/JavaPROG/programs/java_database
Inside this directory I have 3 files
Query.java, Query.class and the database query.db
My java code looks as followed
import java.sql.*;
public class Query3 {
public static void main(String[] args) {
Connection conn = null;
try {
Class.forName("org.sqlite.JDBC");
conn = DriverManager.getConnection("jdbc:sqlite:C:/PROG/JavaPROG/programs/java_database/query.db");
System.out.println("Connection Success");
} catch(Exception log) {
System.out.println("Connection Failed: " + log);
}
}
}
my classpath does contain
C:\Program Files\Java\jdk1.8.0\bin
and inside that bin directory is the sqlite jdbc driver
sqlite-jdbc-3.7.2.jar
I can connect my script to PostgreSQL database but I can't see why it isn't connecting to the SQLite3, I am getting the following error message
java.lang.ClassNotFoundException: org.sqlite.JDBC
have I done something wrong?
You need to explicitly provide the sqlite-jdbc-3.7.2.jar file in your classpath while compiling and running your classes using the -cp option. The java and javac commands will only look for .class files in the classpath and not jars.
Try this :
javac -cp <path_to_jar/sqlite-jdbc-3.7.2.jar>;. <your_class_name>.java
And if you have more than one thirdparty library, you can use a wildcard :
javac -cp <path_to_jar/*>;. <your_class_name>.java
Note that ; is the separator used in Windows. If you are on unix based systems, you need to use : instead of ;. Java maybe platform independant but the java and javac commands are not.
Also note that the . tells the java and javac commands to look in the current directory for classes. Don't forget to provide the classpath to the jars when running your program using the java command.
I have found the answer to fix this.
compile the script as normal
javac Query.java
Then specify the driver when executing
java -classpath ".;sqlite-jdbc-3.7.2.jar" Query
And it will run fine

Java to MySql Connection on Linux

I googled this topic but I have yet to find the answer. I am on AWS Amazon instance running Java trying to run simple java to mysql database. I can access db fine through the mysql command. The error is driving me insane.
Note I am running under /development which happens to have the jar file.
[ec2-user#ip-172-31-16-232 development]$ ls
cProgram.c Java2MySql.class Java2MySql.java mysql-connector-java-5.1.12.jar
[ec2-user#ip-172-31-16-232 development]$ echo $JAVA_HOME
/usr/lib/jvm/java
[ec2-user#ip-172-31-16-232 development]$ java -cp /home/ec2-user/development/mysql-connector-java-5.1.12.jar Java2MySql.java
[ec2-user#ip-172-31-16-232 development]$ java Java2MySql
Where is your MySQL JDBC Driver?
MySQL JDBC Driver Registered!
java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/smarteregsBlog
at java.sql.DriverManager.getConnection(DriverManager.java:596)
at java.sql.DriverManager.getConnection(DriverManager.java:215)
at Java2MySql.main(Java2MySql.java:21)
Closing connection
Java2MySql.java
public class Java2MySql
{
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/myBlog";
String driver = "com.mysql.jdbc.Driver";
String userName = "sam";
String password = "Yoo!";
Connection conn = null;
try {
Class.forName(driver);
}catch (ClassNotFoundException e) {
System.out.println("Where is your MySQL JDBC Driver?");
}
System.out.println("MySQL JDBC Driver Registered!");
try{
conn = DriverManager.getConnection(url,userName,password);
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
System.out.println("Closing connection");
conn.close();
} catch (Throwable ignore){}
}
}
It seems you provide the jar only at compile time and not providing the jar class path when you try to execute the code:
javac -cp /home/ec2-user/development/mysql-connector-java-5.1.12.jar Java2MySql.java
I assume it to be a typo javac and not java
You need to provide the jar path when executing the code as well. So change this
java Java2MySql
to
java -cp .:/home/ec2-user/development/mysql-connector-java-5.1.12.jar Java2MySql
Note: mysql-connector-java-5.1.12.jar is only required at runtime in your case because you are loading the class dynamically.
Make sure the mysql connector jar is located in your classpath. Copying it in same folder as your class file wont work.
Or you can simply copy it to lib folder of your jdk.
Well while compiling the class you don't need to specify the jar in this case so simple this should do the job
javac Java2MySql.java
Change this
java -cp /home/ec2-user/development/mysql-connector-java-5.1.12.jar Java2MySql.java
to
java -cp .:pathOfTheDriverJar/mysql-connector-java-5.1.12.jar Java2MySql

Cannot load native library. Error: java.lang.UnsatisfiedLinkError

I am having trouble getting Java to see the file 'libvensim.so' that I have in my home directory.
I have tried setting LD_LIBRARY PATH...."echo $LD_LIBRARY_PATH" returns "./libvensim.so"
When I run the code:
java -cp ./vensim.jar:. -Djava.library.path=./libvensim.so Test
I get the error "Cannot load native library. Error: java.lang.UnsatisfiedLinkError: no libvensim in java.library.path".
Test.java is a simple class to test whether I can access the .so:
import com.vensim.Vensim;
public class Test {
public static void main(String[] args) throws Exception {
Vensim vensim = new Vensim("libvensim");
}
}
Can anyone see my problem? Thanks very much.
LD_LIBRARY_PATH should point to the directory containing the .so files. Try:
java -cp ./vensim.jar:. -Djava.library.path=. Test
or
export LD_LIBRARY_PATH=/path/to/dir
java -cp ./vensim.jar:. Test

Categories

Resources