jarekbutek jarekbutek - 22 days ago 13
MySQL Question

Could not find or load main class Mysql

I have problem with my java app compilation.

Folder structure:

src/LogServer.java (this is the main class)

src/SocketThread.java

I also use MySQL driver in it.
This files aren't in package.

When I'm running:

javac -classpath mysql-connector-java-3.1.14-bin.jar *.java


everything works fine.

See the compilation:

java -classpath mysql-connector-java-3.1.14-bin.jar LogServer


compiler says:

user@user-Inspiron-3543:~/workspace/LogServer/src$ javac -classpath mysql-connector-java-3.1.14-bin.jar *.java

user@user-Inspiron-3543:~/workspace/LogServer/src$ java -classpath mysql-connector-java-3.1.14-bin.jar LogServer
Error: Could not find or load main class LogServer

user@user-Inspiron-3543:~/workspace/LogServer/src$ java LogServer
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at jdk.internal.loader.BuiltinClassLoader.loadClass(java.base@9-internal/BuiltinClassLoader.java:366)
at jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(java.base@9-internal/ClassLoaders.java:184)
at java.lang.ClassLoader.loadClass(java.base@9-internal/ClassLoader.java:419)
at java.lang.Class.forName0(java.base@9-internal/Native Method)
at java.lang.Class.forName(java.base@9-internal/Class.java:294)
at LogServer.init(LogServer.java:22)
at LogServer.main(LogServer.java:51)
user@user-Inspiron-3543:~/workspace/LogServer/src$

Answer

You will need to use the -classpath argument. In your classpath, you will need '.' to establish that your current directory is on the classpath and the mysql-connector-java-3.1.14-bin.jar. So you put those two together in a list. On Linux, the elements are separated by ':'. On Windows, it is ';'.

java -classpath .:mysql-connector-java-3.1.14-bin.jar LogServer

Edit:massive editing

Comments