views:

1061

answers:

6

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'

+3  A: 

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.

Jason Cohen
+2  A: 

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 ;)

Jon Skeet
javaphild
By the way...I put the javamail .jar in the java.ext directory,and it worked perfectly. A very simple effective solution.My ext directory was c:\program files\java\jdk1.6.0_11\jre\lib\ext
javaphild
A: 

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

webclimber
+1  A: 

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

Schildmeijer
> (On windows its easy to examine a jar file using 7zip.)Use anything that can browse zip-files on any platform, it is not a feature of 7zip.
Fredrik
gah, forgot about comments on stackoverflow ignoring newlines... sorry for that.
Fredrik
A: 

There should be a README.TXT (or similar) included in the JavaMail download, with instructions how to correctly install it.
Or try the README at the JavaMail home page

Carlos Heuberger
A: 

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

Thorbjørn Ravn Andersen