0

I have a folder libs that contains several JARs, and I have an application (let's say MyTest.jar) that uses some classes from those libs. What I need is to run MyTest.jar with a kind of -cp (-classpath) argument that will add all JARs in folder libs to classpath fully.

I have already tried java -cp E:\libs -jar MyTest.jar. However, this doesn't seem to load the libraries at all, as can be seen in this error I get (which states it can't find one of library classes me.darksidecode.simpleconfigs.Config):

Exception in thread "main" java.lang.NoClassDefFoundError: me/darksidecode/simpleconfigs/Config
        at me.darksidecode.KekGUI.<init>(KekGUI.java:31)
        at me.darksidecode.KekGUI.main(KekGUI.java:63)
Caused by: java.lang.ClassNotFoundException: me.darksidecode.simpleconfigs.Config
        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)
        ... 2 more

The line where the exception is thrown is:

Config testcfg = new Config("hello = world");

Where Config (me.darksidecode.simpleconfigs.Config) is a class of the SimpleConfigs.jar library, which is present in my libs (E:\libs) folder and is valid.

To no avail I have also tried specifying one of libraries (SimpleConfigs.jar) explicitly: java -cp E:\libs\SimpleConfigs.jar -jar MyTest.jar. Same error happens.

How can I get this to work?

EDIT: it appears that I have to specify classpath in the MyTest.jar's MANIFEST.MF file. And here I've got a new problem: I can only specify exact JAR name in Class-Path, like this:

Class-Path: /E:/libs/SimpleConfigs.jar

It works like that. But when I try to specify the whole directory:

Class-Path: /E:/libs/

It starts throwing the aforementioned error (java.lang.NoClassDefFoundError) again.

German Vekhorev
  • 339
  • 6
  • 16

1 Answers1

1

The java documentation states

-jar

Executes a program encapsulated in a JAR file. The first argument is the name of a JAR file instead of a startup class name. For this option to work, the manifest of the JAR file must contain a line in the form Main-Class: classname. Here, classname identifies the class with the public static void main(String[] args) method that serves as your application's starting point.

When you use this option, the JAR file is the source of all user classes, and other user class path settings are ignored`.

One workaround is to append MyTest.jar to the classpath (-cp) and specify the main class in the MyTest.jar as argument to java

java -cp Mytest.jar;E:\libs\* me.darksidecode.KekGUI
Baski
  • 829
  • 8
  • 14