简体   繁体   English

在外部Jar中访问类

[英]Accessing classes in an external Jar

I am having problems having my Java programs see classes that are packaged in an external jar. 我在让Java程序查看打包在外部jar中的类时遇到问题。 I am running under Windows 7. I have the classes embedded in a jar called ParserUtilities.jar. 我在Windows 7下运行。我将类嵌入在称为ParserUtilities.jar的jar中。 I established the path with a CLASSPATH variable using the System utility and the Environment tab. 我使用“系统”实用程序和“环境”选项卡使用CLASSPATH变量建立了路径。 I confirmed that the CLASSPATH is set correctly. 我确认CLASSPATH设置正确。 When I type echo %CLASSPATH%, I see C:\\Program Files\\Java\\externaljars\\ParserUtilities.jar which is correct. 当我键入echo%CLASSPATH%时,我看到正确的C:\\ Program Files \\ Java \\ externaljars \\ ParserUtilities.jar。 But when I type java -jar Parse.jar (my executable) I get the error Exception in thread "main" java.lang.NoClassDefFoundError: com/artificialmed/Initialize 但是,当我键入java -jar Parse.jar(我的可执行文件)时,在线程“ main”中收到错误Exception。java.lang.NoClassDefFoundError:com / artificialmed / Initialize

Some additional information: 一些其他信息:

  1. When I put the ParserUtilities.jar in the ..\\lib\\ext directory, everything works. 当我将ParserUtilities.jar放在.. \\ lib \\ ext目录中时,一切正常。
  2. I am running java version 1.6.0_16. 我正在运行Java版本1.6.0_16。 Java(TM) SE Runtime Environment Java(TM)SE运行时环境
  3. In experimenting, I typed java -cp C:\\Program Files\\Java\\jre6\\lib\\ext>java -cp C:\\Program Files\\Java\\externaljars\\ParserUtilities.jar 在实验中,我输入了java -cp C:\\ Program Files \\ Java \\ jre6 \\ lib \\ ext> java -cp C:\\ Program Files \\ Java \\ externaljars \\ ParserUtilities.jar

and got this error "Could not find the main class: Files\\Java\\externaljars\\ParserUtilities.jar" but there is no main class (its just a collection of classes I use in a bunch of programs). 并收到此错误“找不到主类:Files \\ Java \\ externaljars \\ ParserUtilities.jar”,但没有主类(它只是我在一系列程序中使用的类的集合)。

I do not have a Java SDK loaded in the environment, just a java JRE (Is this the issue?). 我没有在环境中加载Java SDK,只有Java JRE(这是问题吗?)。

Try adding "" around the classpath entry since you have a space in the directory (Program Files). 尝试在类路径条目周围添加“”,因为目录(程序文件)中有空格。 The error message "Files\\Java\\externaljars\\ParserUtilities.jar" would indicate that is the problem. 错误消息“ Files \\ Java \\ externaljars \\ ParserUtilities.jar”将表明存在此问题。

From Sun's documentation on the -jar option to the java command: "When you use this option, the JAR file is the source of all user classes, and other user class path settings are ignored." Sun的 -jar选项到java命令的文档 :“使用此选项时,JAR文件是所有用户类的源,而其他用户类路径设置将被忽略。” I believe that means that both your CLASSPATH environment variable as well as any -cp arguments that you might provide on the command line are both going to be ignored. 我相信这意味着您的CLASSPATH环境变量以及您可能在命令行上提供的任何-cp参数都将被忽略。

So, you have the following options: 因此,您有以下选择:

  1. Package everything into one jar, and then you can execute it by running java -jar JarWithEverything.jar . 将所有内容打包到一个jar中,然后可以通过运行java -jar JarWithEverything.jar来执行它。
  2. Keep things in separate jars and provide both jars as arguments on the command line, so that you type something like this: 将内容保存在单独的jar中,并在命令行上将这两个jar作为参数提供,以便您键入以下内容:

    java -cp "C:\\Program Files\\Java\\externaljars\\ParserUtilities.jar";"C:\\Program Files\\Java\\externaljars\\Parser.jar" com.artificialmed.Initialize

  3. Keep things in separate jars and put one or both jars in your CLASSPATH environment variable instead of providing them on the command line. 将内容保存在单独的jar中,然后将一个或两个jar放在CLASSPATH环境变量中,而不是在命令行中提供它们。

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

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