简体   繁体   中英

add library to java, CLASSPATH, jar, linux

I've been browsering the Internet for a couple of hours and I am unable to find an answer to my question. The library I'm trying to add is JGraphT

I'm new to Java and I wanted to add a free graph library. I downloaded all .jar files and then the issues startet. What step by step should I do? I found information about compiling with -cp or -classpath or addng .jar to CLASSPATH (I'm using Linux and I write my programms in gedit (obligatory for my studies) and compile it with terminal). But I wonder what should I do step by step?

What do I have so far:

  • I have downloaded multiple .jar and they all sit in one folder with
    the xxx.java file I would like to compile

  • do I need to change CLASSPATH? How to do it? How should I compile and run my program after changing CLASSPATH? The ordinary way(javac
    xxx.java; java xxx) or should I change sth?

  • or maybe I don't need to change CLASSPATH just add -classpath while compiling? If so, what should the compile anr run commend look
    like?

Also I have already tried using -cp... I'm enclosing my lines in terminal. It compiled correctly, but when I tried to run it, I received strange errors. I'm sure the code is correct since it was given in the library as a way to test wether or not is it installed correctly.

wiktoria@wiktoria-1015PW:~/programowanie/grafy/java/testy$ javac -cp jgrapht-ext-0.9.1-uber.jar: HelloJGraphT.java
wiktoria@wiktoria-1015PW:~/programowanie/grafy/java/testy$ java -cp jgrapht-ext-0.9.1-uber.jar: HelloJGraphT
Exception in thread "main" java.lang.NoClassDefFoundError: HelloJGraphT (wrong name: org/jgrapht/demo/HelloJGraphT)
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
    at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
    at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:482)
wiktoria@wiktoria-1015PW:~/programowanie/grafy/java/testy$ 

Let's say your class belongs into the package net.example.graph. This means the real name of the class is net.example.graph.HelloJGraphT .

This also means you have a directory structure like this:

project_dir/net/example/graph

Go to the project_dir folder, then try this:

javac -cp <path to jgrapht JAR> net/example/graph/HelloJGraphT.java
java -cp <path to jgrapht JAR> net.example.graph.HelloJGraphT 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM