简体   繁体   English

我可以将类添加到sun的rt.jar文件中吗?

[英]Can I add classes to sun's rt.jar file?

I downloaded the Javax.mail package. 我下载了Javax.mail软件包。 I have jdk1.6.0_11. 我有jdk1.6.0_11。 Problem is...I cannot get javac or java to find those classes! 问题是...我无法让javac或java找到那些类! I can get apps to compile using JCreator LE ( by adding the mail jar to its search list ) but, when I try to run the app in a command window, it fails. 我可以使用JCreator LE(将邮件罐添加到其搜索列表中)来编译应用程序,但是,当我尝试在命令窗口中运行该应用程序时,它会失败。

Can I add these new classes to the rt.jar without hurting my jdk installation? 我可以在不损害jdk安装的情况下将这些新类添加到rt.jar吗? I know java has it wired up to look there for classes. 我知道java已将其连接起来以在那里查找类。 (And, the mail classes are inside a javax package - seems like they could reasonably be added to the javax folder in rt.jar.. (而且,邮件类在javax包中-似乎可以将它们合理地添加到rt.jar中的javax文件夹中。

Thanks! 谢谢! Phil D' 菲尔·达德

No you can't, nor should you. 不,你不能,你也不应该。

Instead, figure out the problem with your classloader (probably paths?). 相反,找出类加载器的问题(可能是路径?)。 You'll need that for the next library you need to access. 对于下一个需要访问的库,您将需要它。

Messing with rt.jar means you can't run on any other JVM. rt.jar的混乱意味着您不能在任何其他JVM上运行。

You should either specify the jar file in your classpath: preferably on the command line with the -cp option, but possibly with the CLASSPATH environment variable. 您应该在类路径中指定jar文件:最好在命令行中使用-cp选项,但也可以使用CLASSPATH环境变量。

Alternatively, you can specify its directory in the java.ext.dirs system property. 或者,您可以在java.ext.dirs系统属性中指定其目录 For more details, see the documentation for the extensions mechanism . 有关更多详细信息,请参见扩展机制文档

You shouldn't be messing around with rt.jar . 您不应该在rt.jar周围弄乱。 That's very definitely not the way to make extra jar files available - it's akin to trying to add Microsoft Word to the Windows kernel ;) 这绝对不是使额外的jar文件可用的方法-类似于尝试将Microsoft Word添加到Windows内核;)

Adding things to rt.jar seems like a bad idea, even though its possible and easy to accomplish. 向rt.jar添加内容似乎是个坏主意,即使它可能且容易实现。

Try compile your application from the command line like this: 尝试从命令行编译您的应用程序,如下所示:

javac -cp <path_to_3rd_libs>/jarfile.jar . MainClass.java

If the compiler still complains about the javax.mail package try to unpack/examine the jar file to see that javax.mail package (and its expected content) is there. 如果编译器仍然抱怨javax.mail软件包,请尝试解压缩/检查jar文件,以查看是否存在javax.mail软件包(及其预期的内容)。

(On windows its easy to examine a jar file using 7zip.) (在Windows上,使用7zip可以轻松检查jar文件。)

Most definitely no. 绝对没有。 If you post the command you are running from the command line we will be able to point you on the right direction, but most likely you are just missing a classpath parameter. 如果您从命令行运行您正在运行的命令,我们将为您指明正确的方向,但是很可能您只是缺少一个classpath参数。

java -classpath /path/to/mail.jar MyClass java -classpath /path/to/mail.jar MyClass

You need to understand the CLASSPATH concept which allows you to add individual classes and jar files containing classes to the "universe" of defined classes available for the code you want to compile and/or run. 您需要了解CLASSPATH概念,该概念允许您将单独的类和包含类的jar文件添加到已定义类的“ Universe”中,这些“类”可用于要编译和/或运行的代码。 It is similar in idea to the PATH variable in the Windows world. 它的概念类似于Windows世界中的PATH变量。

For the Windows command line this is the documentation: 对于Windows命令行,这是文档:

http://java.sun.com/javase/6/docs/technotes/tools/windows/classpath.html http://java.sun.com/javase/6/docs/technotes/tools/windows/classpath.html

The Java Tutorial surprised me by not being well-written for this particular concept: Java教程没有为这个特定概念写得很好,这使我感到惊讶:

http://java.sun.com/docs/books/tutorial/essential/environment/paths.html http://java.sun.com/docs/books/tutorial/essential/environment/paths.html

You most likely need something along the lines: 您很可能需要以下内容:

C:> set CLASSPATH=c:\\javamail\\first.jar;c:\\javamail\\second.jar C:>设置CLASSPATH = c:\\ javamail \\ first.jar; c:\\ javamail \\ second.jar

after which both java and javac should know about these classes 之后,java和javac都应该知道这些类

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

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