访问外部Jar中的类 [英] Accessing classes in an external Jar

查看:125
本文介绍了访问外部Jar中的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在让Java程序查看打包在外部jar中的类时遇到问题.我在Windows 7下运行.我将类嵌入在称为ParserUtilities.jar的jar中.我使用系统"实用程序和环境"选项卡使用CLASSPATH变量建立了路径. 我确认CLASSPATH设置正确.当我键入echo%CLASSPATH%时,我看到 C:\ Program Files \ Java \ externaljars \ ParserUtilities.jar是正确的.但是当我打字 java -jar Parse.jar(我的可执行文件)出现错误 线程主"中的异常java.lang.NoClassDefFoundError:com/artificialmed/Initialize

I am having problems having my Java programs see classes that are packaged in an external jar. I am running under Windows 7. I have the classes embedded in a jar called ParserUtilities.jar. I established the path with a CLASSPATH variable using the System utility and the Environment tab. I confirmed that the CLASSPATH is set correctly. When I type echo %CLASSPATH%, I see C:\Program Files\Java\externaljars\ParserUtilities.jar which is correct. But when I type java -jar Parse.jar (my executable) I get the error Exception in thread "main" java.lang.NoClassDefFoundError: com/artificialmed/Initialize

一些其他信息:

  1. 当我将ParserUtilities.jar放在.. \ lib \ ext目录中时,一切正常.
  2. 我正在运行Java版本1.6.0_16. Java(TM)SE运行时环境
  3. 在实验中,我键入了java -cp C:\ Program Files \ Java \ jre6 \ lib \ ext> java -cp C:\ Program Files \ Java \ externaljars \ ParserUtilities.jar

并收到此错误找不到主类: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).

我没有在环境中加载Java SDK,只有Java JRE(这是问题吗?).

I do not have a Java SDK loaded in the environment, just a java JRE (Is this the issue?).

推荐答案

来自 Sun的文档关于java命令的-jar选项:使用此选项时,JAR文件是所有用户类的源,而其他用户类路径设置是忽略了."我相信这意味着您的CLASSPATH环境变量以及您可能在命令行上提供的任何-cp参数都将被忽略.

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." 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.

因此,您有以下选择:

  1. 将所有内容打包到一个jar中,然后您可以通过运行java -jar JarWithEverything.jar来执行它.
  2. 将它们保存在单独的jar中,并在命令行上将这两个jar作为参数提供,以便您键入以下内容:

  1. Package everything into one jar, and then you can execute it by running 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:

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

将它们放在单独的jar中,然后将一个或两个jar放在您的CLASSPATH环境变量中,而不是在命令行中提供它们.

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中的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆