Search code examples
javascalacommand-lineclassnotfoundexceptionexecutable-jar

How to add jar file into the class-path while running another jar file during run time


I have a jar file(mts.jar) to run and it requires another jar(hook.jar) to be added at the runtime.

mts.jar is dependent on some of the classes present in hook.jar. And our requirement is to pass this hook.jar in classpath at runtime while running mts.jar.

I have tried the combinations below:

java -cp "bin:hook.jar" mts.jar ccp.mts.server.websocket.Server

java -cp "hook.jar" mts.jar ccp.mts.server.websocket.Server

I have also tried to set the classpath like below:

set CLASSPATH="C:\Users\Desktop\mts 4.3\hook.jar"

then tried running as usual like below :

java -cp mts.jar ccp.mts.server.websocket.Server

But I am getting class not found exception.

Where I might be wrong ?


Solution

  • Make sure both files are inside the same directory, then call it like this

    java -cp mts.jar;hook.jar ccp.mts.server.websocket.Server

    You can also specify whole path, e.g.:

    java -cp c:\mts.jar;c:\hook.jar ccp.mts.server.websocket.Server

    To run your application, make sure that class: ccp.mts.server.websocket.Server is located inside one of the JAR files and has main method.