简体   繁体   中英

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

I downloaded the Javax.mail package. I have jdk1.6.0_11. Problem is...I cannot get javac or java to find those classes! 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.

Can I add these new classes to the rt.jar without hurting my jdk installation? I know java has it wired up to look there for classes. (And, the mail classes are inside a javax package - seems like they could reasonably be added to the javax folder in rt.jar..

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.

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.

Alternatively, you can specify its directory in the java.ext.dirs system property. For more details, see the documentation for the extensions mechanism .

You shouldn't be messing around with 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 ;)

Adding things to rt.jar seems like a bad idea, even though its possible and easy to accomplish.

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.

(On windows its easy to examine a jar file using 7zip.)

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.

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. It is similar in idea to the PATH variable in the Windows world.

For the Windows command line this is the documentation:

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:

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

after which both java and javac should know about these classes

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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