Run .class with library from cmd

0

Before I asked about how to execute a class here

In summary the sentence that has worked for me has been:

java -cp . com.index.Clase

The problem is that when executing that sentence, it throws an exception saying that it does not find one of the several libraries that I use:

Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/jersey/api/client/Client
        at java.lang.Class.getDeclaredMethods0(Native Method)
        at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
        at java.lang.Class.privateGetMethodRecursive(Unknown Source)
        at java.lang.Class.getMethod0(Unknown Source)
        at java.lang.Class.getMethod(Unknown Source)
        at sun.launcher.LauncherHelper.validateMainClass(Unknown Source)
        at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
Caused by: java.lang.ClassNotFoundException: com.sun.jersey.api.client.Client
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        ... 7 more 

Looking through stackOverflow in I found this example:

java -cp c:\location_of_jar\myjar.jar com.mypackage.myClass

In my case following the structure:

C:
\Programa
 |   \com
 |       \index
 |           Clase.class
 jersey-client-1.9.1.jar

Execute:

C:\Programa>java -cp jersey-client-1.9.1.jar . com.index.Clase

He throws me again the same exception as at the beginning.

By the way I'm not sure what the . means but without it it does not work for me.

    
asked by nachfren 15.07.2018 в 15:39
source

1 answer

1

To use external libraries you must also include them in the classpath , this depends of course on the structure of the project; taking the one of the example that you mention, it is necessary to be placed in the root of the project Programa and then:

java -cp ;jersey-client-1.9.1.jar com.index.Clase

After java -cp goes where the .class is located and also the external libraries that the app will use ( by placing a ; for Windows em> o : for * nix ).

For this specific case, where the com.index package is directly in the root of the project, just like the JAR, it is enough to 'leave blank' the location of .class , separate and then put the name of the JAR.

Now if for example we assume that the package com.index were inside a directory org/user and in turn the JAR belonged to a directory lib/tools :

Programa
|--org
|  |--user
|     |--com
|        |--index
|           |-- Clase.class
|--lib
|  |--tools
|     |-- jersey-client-1.9.1.jar

the instruction by console ( Windows ) would be equivalent to:

java -cp org\user;lib\tools\jersey-client-1.9.1.jar com.index.Clase

    
answered by 16.07.2018 / 02:51
source