• Post Reply Bookmark Topic Watch Topic
  • New Topic

Adding Jar Files to java ClassPath  RSS feed

 
Raj Aditya
Greenhorn
Posts: 21
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually I want to use JGraphs. So I added the executable jar file to eclipse and the code was working but when i am running it from command prompt JNI error comes up. can someone please help?
 
Stephan van Hulst
Saloon Keeper
Posts: 7986
143
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to CodeRanch!

What is the command you are running?
 
Raj Aditya
Greenhorn
Posts: 21
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I command javac -cp "<the path>" file.java for compilation and java <ClassName> for running the program and when i run the command java <ClassName> JNI error comes up.
 
Stephan van Hulst
Saloon Keeper
Posts: 7986
143
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You still need to tell the java command where to find the dependency. You can do that with the -classpath option.
 
Raj Aditya
Greenhorn
Posts: 21
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh but what else to add in the java command
 
Stephan van Hulst
Saloon Keeper
Posts: 7986
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean?

If it's not working, please tell us the *exact* command you used, and copy the error message.
 
Raj Aditya
Greenhorn
Posts: 21
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The commands and the error messages are shown below

C:\Users\Raj\Documents\4th year\sem 1\Information retrieval\IR Project\jgraphx-master\examples\com\mxgraph\examples\swing>javac -cp "C:\Program Files\Java\jdk\adi\jgraphx.jar" HelloWorld.java
                                                                                C:\Users\Raj\Documents\4th year\sem 1\Information retrieval\IR Project\jgraphx-master\examples\com\mxgraph\examples\swing>java HelloWorld
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: com/mxgraph/swing/mxGraphComponent
        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.mxgraph.swing.mxGraphComponent         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

 
Stephan van Hulst
Saloon Keeper
Posts: 7986
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You didn't use the -classpath option as I explained before.

Try: java -classpath "C:\Program Files\Java\jdk\adi\jgraphx.jar" HelloWorld

Unrelated to your question: Why is jgraphx.jar underneath your JDK directory? Why is the HelloWorld class in the default package?
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't need to add .; or ;. to that classpath?
 
Raj Aditya
Greenhorn
Posts: 21
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry I exactly did not get your question. jgraphx.jar is inside the directory "adi" which is inside JDK directory
 
Raj Aditya
Greenhorn
Posts: 21
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I try java -classpath "C:\Program Files\Java\jdk\adi\jgraphx.jar" HelloWorld it says that could not load or find the class HelloWorld
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try .; prepended to the beginning of the classpath, or ;. appended to its end. You most probably don't need both.
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan means you oughtn't to put your on work, nor your downloaded .jars, in the Java® installation folders.
 
Raj Aditya
Greenhorn
Posts: 21
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot. It finally worked after putting .; in the start.
Can you also please explain why is it necessary to prepend it with that?
 
Raj Aditya
Greenhorn
Posts: 21
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So is it necessary to add the classpath even if the java file and the jar file are in the same directory?
 
Stephan van Hulst
Saloon Keeper
Posts: 7986
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. One classpath entry can only refer to a folder with classes or a jar with classes. A single period refers only to the current folder, not any jars inside it.
 
Paul Clapham
Sheriff
Posts: 22828
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually... I think it was in Java 6 that they added classpath wildcards. So you can specify all jars in a particular directory using a wildcard -- here's a link to the part of the tutorial where it says that: Class Path Wild Cards.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!