2

I referenced one external library (Opencsv) in my project.

I added the Manifest file into the 'src' folder as:

Manifest-Version: 1.0
Main-Class: DataClean.DataPreprocess
Class-path: jars/opencsv.jar

The 'jars' is a folder contains the opencsv jar file in 'src' folder.

The complied main class is in 'DataClean' folder in 'src'.

When I create the jar, I use:

jar cfm PromoPrice_CN.jar Manifest.txt *

This command was run within 'src' folder.

Then, the JAR I created can only run within this 'src' directory. Otherwise, it gives the exceptions:

Exception in thread "main" java.lang.NoClassDefFoundError: com/opencsv/CSVReader
    at DataClean.DataPreprocess.dataClean(DataPreprocess.java:154)
    at DataClean.DataPreprocess.main(DataPreprocess.java:229)
Caused by: java.lang.ClassNotFoundException: com.opencsv.CSVReader
    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 command I used to run the jar is:

java -jar C:\...\src\PromoPrice_CN.jar X Y Z

X, Y and Z are three input parameter for the jar.

Please help where did I do wrong?

FYI, the creation of the JAR has to be in command line for other reasons.

Thx

NinjaNinja
  • 21
  • 3
  • Which java version are you using? – Fairoz Jul 20 '16 at 09:46
  • make sure, that your manifest file contains empty last line, also try "Class-Path" (with capital P) – commit-man Jul 20 '16 at 09:51
  • @commit-man tried...still had exception... – NinjaNinja Jul 20 '16 at 10:12
  • Well, you can try to debug and find problem yourself. Each classloader (instance of URLClassLoader) has ucp field ( which is URLClassPath), each ucp has list of urls. You can see details here `sun.misc.URLClassPath#getLoader(int)`. For each url loader should be created and placed in lmap (loaders map). Class-Path manifest entry is parsed here `sun.misc.URLClassPath.JarLoader#getClassPath`. Insert breakpoints and check if urls are correct – commit-man Jul 20 '16 at 11:42

0 Answers0