简体   繁体   English

将库添加到Java,CLASSPATH,JAR,Linux

[英]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 我要添加的库是JGraphT

I'm new to Java and I wanted to add a free graph library. 我是Java的新手,我想添加一个免费的图形库。 I downloaded all .jar files and then the issues startet. 我下载了所有.jar文件,然后启动了问题。 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). 我找到了有关-cp或-classpath或addng .jar到CLASSPATH进行编译的信息(我正在使用Linux,并且我在gedit中编写了程序(用于学习),并使用终端进行了编译)。 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 我已经下载了多个.jar,它们都放在一个文件夹中,
    the xxx.java file I would like to compile 我要编译的xxx.java文件

  • do I need to change CLASSPATH? 我需要更改CLASSPATH吗? How to do it? 怎么做? How should I compile and run my program after changing CLASSPATH? 更改CLASSPATH后应如何编译和运行程序? The ordinary way(javac 普通方式(javac
    xxx.java; xxx.java; java xxx) or should I change sth? java xxx)还是应该更改某项?

  • or maybe I don't need to change CLASSPATH just add -classpath while compiling? 或者也许我不需要更改CLASSPATH,只需在编译时添加-classpath即可? 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. 另外,我已经尝试使用-cp ...将行封闭在终端中。 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. 假设您的课程属于net.example.graph包。 This means the real name of the class is net.example.graph.HelloJGraphT . 这意味着该类的真实名称是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: 转到project_dir文件夹,然后尝试以下操作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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