简体   繁体   English

如何在Ubuntu上使用apache commons java库?

[英]How to use the apache commons java library on Ubuntu?

I am a Java beginner and trying to figure out how to use the apache commons lib. 我是一个Java初学者,并试图弄清楚如何使用apache commons lib。

Here is a source file Randstr.java : 这是一个源文件Randstr.java

import org.apache.commons.lang3.RandomStringUtils;

class Randstr {
    public static void main(String[] args) {
        String s = RandomStringUtils.random(12);
        System.out.println(s);
    }
}

I have the commons-lang3-3.1.jar file in /usr/share/java/ and have created a symlink in the current dir. 我在/ usr / share / java /中有commons-lang3-3.1.jar文件,并在当前目录中创建了一个符号链接。 Then I compiled it like this: javac -cp commons-lang3-3.1.jar Randstr.java , the complilation was fine, but when I execute java Randstr , I got the following error: 然后我编译它像这样: javac -cp commons-lang3-3.1.jar Randstr.java ,complilation很好,但是当我执行java Randstr ,我收到以下错误:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/lang3/RandomStringUtils
        at Randstr.main(Randstr.java:5)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.lang3.RandomStringUtils
        at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
        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:424)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
        ... 1 more

And if I don't specify the jar file in the classpath, it will not even compile: 如果我没有在类路径中指定jar文件,它甚至不会编译:

javac -cp . Randstr.java

# Randstr.java:1: error: package org.apache.commons.lang3 does not exist
# import org.apache.commons.lang3.RandomStringUtils;
#                                ^
# Randstr.java:5: error: cannot find symbol
#         String s = RandomStringUtils.random(12);
#                    ^
#   symbol:   variable RandomStringUtils
#   location: class Randstr
# 2 errors

javac -cp /usr/share/java/  Randstr.java

# Randstr.java:1: error: package org.apache.commons.lang3 does not exist
# import org.apache.commons.lang3.RandomStringUtils;
#                                ^
# Randstr.java:5: error: cannot find symbol
#         String s = RandomStringUtils.random(12);
#                    ^
#   symbol:   variable RandomStringUtils
#   location: class Randstr
# 2 errors

From reading other questions on stackoverflow, I see this can be solved by using an IDE, but I prefer a simple editor at the moment. 从阅读stackoverflow上的其他问题,我看到这可以通过使用IDE来解决,但我现在更喜欢一个简单的编辑器。

If you can compile it with 如果你可以编译它

javac -cp commons-lang3-3.1.jar Randstr.java

then you can run it with 然后你可以运行它

java -cp commons-lang3-3.1.jar:. Randstr

The JAR file has to be in the classpath. JAR文件必须位于类路径中。

Edit your profile file. 编辑您的个人资料文件 vim ~/.bashrc vim~ / .bashrc

In your profile file add the following line: 在您的个人资料文件中添加以下行:

export CLASSPATH=/usr/share/java/commons-lang3-3.1.jar:.

Log out and back in. Or source your profile file in the windows you have open. 注销并重新登录。或者在已打开的窗口中找到您的个人资料文件。 You can always add your classpath to every java and javac command you invoke but that becomes a pain. 您始终可以将类路径添加到您调用的每个java和javac命令中,但这会变得很麻烦。 With the CLASSPATH environmental variable you don't have to add it on the command line any more. 使用CLASSPATH环境变量,您不必再在命令行上添加它。 Note that if you are using an IDE such as NetBeans or Eclipse you still might have to add the library to your project's libraries within the IDE. 请注意,如果您使用的是NetBeans或Eclipse等IDE,则可能还需要将库添加到IDE中项目的库中。

Clearly the contents of /usr/share/java/ don't automatically get added to the classpath - it's just a common location where APT packages put Java libraries. 显然, /usr/share/java/的内容不会自动添加到类路径中 - 它只是APT包放置Java库的常用位置。 It's up the developer to reference them correctly. 开发人员可以正确引用它们。

JARs in the ext/ subdirectory of a Java installation do get added to the classpath automatically. Java安装的ext/子目录中的JAR会自动添加到类路径中。 However, do not put your own JARs in there. 但是, 不要将自己的JAR放在那里。 It's a terrible practice because it doesn't match how Java apps are deployed "in the real world". 这是一种可怕的做法,因为它与“在现实世界中”部署Java应用程序的方式不相符。

The correct way is using the -cp parameter explicitly when compiling AND running your app. 正确的方法是在编译和运行应用程序显式使用-cp参数。 Java doesn't compile library code into your .class files, a .class file only refers to names of other classes which are then loaded as-needed from the class path when your app runs. Java不会将库代码编译到.class文件中, .class文件只引用其他类的名称,然后在应用程序运行时根据需要从类路径加载。 The -cp parameter takes only .jar files, or directories with .class files in them. -cp参数仅包含.jar文件或包含.class文件的目录。 You can also use wildcards in the value of that parameter. 您还可以在该参数的值中使用通配符。 For more information on wrangling the class path, check the tool documentation on setting the class path . 有关对类路径进行争论的更多信息,请查看有关设置类路径工具文档

You using a build tool that sets it for you automatically, like an IDE or Maven or another build system with dependency management. 您使用自动为其设置的构建工具,如IDE或Maven或具有依赖关系管理的其他构建系统。 ( Gradle or Ant+Ivy .) If you're writing a Java app that uses third party libraries, I very strongly suggest you learn and use one of those. GradleAnt + Ivy 。)如果你正在编写一个使用第三方库的Java应用程序,我强烈建议你学习并使用其中的一个。 (Also, most IDEs can work with Maven's configuration files letting you use the same build settings in a team with people using mixed or no IDEs.) Generally if you're invoking a compiler directly you're not doing it right. (此外,大多数IDE可以使用Maven的配置文件,让您在团队中使用相同的构建设置,人们使用混合IDE或不使用IDE。)通常,如果您直接调用编译器,则表明您没有正确执行。

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

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