One fine day, I was creating a mini project in Java. The concept of the project was to demonstrate the CRUD operation using Swing, i.e. using GUI. As it was to demonstrate CRUD, I used JDBC in the program as well. If you are familiar with JDBC then you must be knowing that it requires a JDBC driver’s JAR file to successfully compile and execute a JDBC program.
I followed all the steps, wrote a clean code and added the JAR file in the root directory of the project. I compiled the program using the following command:
javac -cp “.;relative/path/to/jar/file” filename.java
The compilation was successful. Then I tried to execute the program with the following command:
java -cp “.;relative/path/to/jar/file” classname
But instead of successfully executing, it displayed the following error:
Error: Could not find or load main class classname
Caused by: java.lang.ClassNotFoundException: classname
I googled a lot about the right procedure to compile and execute a JDBC program but either the result contained the same procedure or contained the steps to program on IDE like NetBeans.
After doing a lot of research I went to Stackoverflow, and from the answers there I concluded that I had done a slight mistake in the commands. I was specifying the relative path to the JAR file whereas I was supposed to provide a fully qualified path of the JAR file, i.e. the absolute path for the file. Also, it didn’t require to specify classpath while compiling as the JAR file was in the same directory.
So the new compiling and executing command goes like this:
javac filename.java
java -cp “.:fully/qualified/path/to/jar/file” classname
Another thing to notice here is that I changed the semicolon(;) with a colon(:) in the classpath to separate two different classpaths. Because Linux based OS uses the colon as a separator but Windows uses a semicolon as a separator as the colon is used for drive specification in the Windows system.
Top comments (0)