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

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

Related

What are Java Classpath and Path?

I have been googling around, trying to understand what the Java Classpath and Path are. However, I am stil not quite sure if I have understood it or not. If feel that this topic is one of those grey areas.
Can someone explain me what those are? I mean, where do I find and set them (where is the actual text file on Mac/Windows)? Is there only one instance of each one? If so, how do I set the path for multiple classes?
As you might have notices, I am totally confused right now after reading so many different tutorials... So now I really would like to have a straight forward explanation.
Please help me, I just trying to learn :)
Thank you all
A path is just a folder location. The path is where your OS will look for programs by default. If java, javac, javap, etc, etc, are in your path then you can just type their names without the entire folder location.
Your classpath is similar. It is a set of folders that contain .class files describing classes(hence the name) and .jar files, which are basically files that contain .class files. All code that you're running is either out of the classpath, generated, or out of the java libaries(also part of the classpath, techncically).
With each run of a java program you can specify a classpath by parameters passed to the java executable. It also grabs classes out of "extension folders,", special folders Java keeps around to act as a system classpath, and finally, the "bootstrap classes", which are a set of important classes almost any Java program needs to run.
Simple mean of path is location of file system. if you want to access any file then you have to manually needs to go there location.
just example: d:\text1.txt then needs to go that d:\ location. same way java program have command like
javac -for compile
java - for run
.
.
.
etc.
that inside java-jdk\bin folder
so if you don't set into classpath. then you can execute java program like
run->cmd
c:\jdk1.6\bin> javac test.java
so without going explicit way you can set it into classpath, and direct execute java program from anywhere.
You can set java path as environment variable of computer.
The PATH is basically where your JDK is installed; this is essentially what your IDE will look for when trying to compile or create Javadoc or such; it's basically just the location of a folder on your hard drive, set as a Windows (or other OS) environment variable to make it easier to use.
The CLASSPATH is a property that tells the compiler where to look for classes. Basically if you download a library or such from somewhere, you need to add it to the CLASSPATH for the compiler to use it. Usually you can do this in your IDE, however, you should not need to directly access the CLASSPATH variable.
By the way, the Wikipedia article is pretty helpful.
1)java Path: it is location of binary executable files
example :javac , java
this file are used for compile and run
2)class Path: it is location of .class file(file create after compile your source code .java file)

Have a difficulty with installing a .jar library

I have just downloaded a third party java library which i need for a program i'm about to create.
But i can't figure out how to actually install the library so that i can literally type
import path.to.library;
in my java class file without having any errors.
I have looked at many tutorials and answers on StackOverflow but each of them seems to include the use of some or the other IDEs for java.
Well, i'm a bit rustic and would like to know how to make it work with notepad and the command line, coz that's what i use to make a program.
When you are compiling, include the following in your line:
-classpath nameOfJar.jar
However, once you actually switch to use an IDE, you will see the multiple benefits this approach can bring.
You don't specify a path in the import statement, just the package name.
All usable JAR files have to be specified in the classpath on commandline when starting your Java program.
You need to understand how Java's classpath works. For a comprehensive description, read the Oracle manual page on this topic. Alternatively the PATH and CLASSPATH page of the Java Tutorial.
(FWIW - it is generally considered to be a bad idea to use the CLASSPATH environment variable to set the classpath, because this is liable to lead to "nasty surprises" if you deal with software that requires different classpaths.)
If you don't use any IDE you won't have code complete. However if you know all the packages/classes/methods names/signitures you can use pure Notepad and then compile it by adding the library to your classpath (eg. using the -cp switch in the javac command when compiling)
JARs are not required to be installed. They required to be accessible at compile- and run- time. You can add jar by command line parameter or CLASSPATH environment variable. IDEs have special means for setting JAR;s location in visual manner.
You'll need to be more specific about what you have tried so far. This generally isn't something complex though, if you want to manually invoke the compiler you would do something like
javac -cp somejar.jar myclass
Once you get used to this process, it's better to automate it using a build tool such as ant or maven. Ant is a little easier to begin with, maven has some additional capabilities that make it a little more complex.

How to update rt.jar file?

I want to add some class files to rt.jar. How am I able to do that?
Your question indicates you have some misunderstanding of the java platform.
First of all you need to know what the rt.jar is and what it does:
rt.jar is the jar that contains all the classes necessary for the java runtime. Hence it's name rt.jar
Now that you know that, you need to know how your java program runs:
Your java program, all your jars and classes are executed by the java virtual machine.
So as you can see the code you write & the rt.jar which is used by the java run time are completely separate and should remain so.
If you need some functionality you should add it to your jar.
Do not update it. Why do you want to update it?
Well anyhow if you want to update it, I know one way, You can open jar file in winrar and paste updated .class files in archive. But your jar may get in inconsistent state. Do it at your own risk.
You can use this command:
jar uf jar-file input-file(s)
Refer the link for details:
java.sun.com/docs/books/tutorial/deployment/jar/update.html
The best way to update the rt.jar is to install a newer version for Java. ;)
If you want to add your own classes in new packages, you can add these to a jar which is in your class path. This is preferable to changing existing classes.
If the only option is to change existing classes, you can create a "patch" in a jar which you prepend to your boot class path, or you can add the jar to an lib/endorsed directory. I wouldn't do this for production code, only for your own testing.
I've tried jar.exe with the u and 0 (i.e. zero) options and that gets the closest to looking like the original rt.jar file but if I've updated the JDK's JRE's rt.jar I have problems with compiling and jarring after the update. No idea why! Simply running a program with the JRE seems to work.
I also tried -Xbootclasspath/p but couldn't get it to work.
Looking at Replace a class within the Java class library with a custom version I see that there are legal problems with distributing an altered rt.jar to your customers, even if you could figure out how to do it correctly. So I plan to take the advice in that page and create a java agent. That's apparently legal and works.
One reason a person might want to modify rt.jar is to add debugging information after compiling the source that comes with the JDK with the -g option. One may also want to patch something. These would be for one's own use, of course.

Compiling Java package throws errors for external Jars

Pretty basic problem here. So I have a Java package that I have created that has three classes (one has the main method). I am trying to use a few Apache Jars, and have added these to my build path in Eclipse. However Eclipse wont let me build and run it properly, so I am trying the command line. I have added the env var CLASSPATH and pointed it to my lib directory which hold the Apache Jars. However, when I try to use javac I get a bunch of errors:
package org.apache.xmlrpc does not exist
import org.apache.xmlrpc.client.XmlRpcClient;
I was reading the man page for javac and it said that:
If neither CLASSPATH, -cp nor -classpath is specified, the user class path consists of the current directory.
So I tried copying the Jars to the same location as my three source files, but no change.
Can someone please tell me what I'm doing wrong?
Thanks.
Classpath variable (or command line option of javac) must contain all jars explicitly. It cannot go through jar files stored in specified directory.
You can compile this by specifying the option -cp on the command line:
javac -cp foo.jar:bar.jar foo/bar/Baz.java
You then run it with the same option:
java -cp foo.jar:bar.jar foo.bar.Baz
It sounds like you've just set the classpath to the directory containing the jar files. You need to set it to the individual jar files, or use java.ext.dirs to set an "extension" directory containing jar files. I'd recommend using the specific jar files. Something like:
// Assuming Windows...
CLASSPATH = c:\libs\foo.jar;c:\libs\bar.jar
I'd also personally recommend specifying the classpath on the command line instead of using an environment variable - the latter will work, but it ends up being a bit more fiddly if you want to compile different projects against different libraries.
However, I'd actually recommend getting Eclipse working first, rather than retreating to the command line. It should be fine - if you could give us more information about what's failing in Eclipse, we may be able to help you with that instead.
The jar files in the current directory are not automatically included; that only refers to .class files in normal package/directory hierarchy. Jar files must be added either explicitly, or via a wildcard like javac -cp ./* (Assuming JDK6+)
(Some OSes may require an escape of the * to avoid globbing; OSX does not.)
I agree with previous answers, but I would also recommend to use proper java build tool - like ant (perceived easier to use, but not necessary) or maven ( perceived more difficult to use, but really worth learning )

Reference all jars from a folder

I'm executing a java application in DOS command window using something like
java -cp abcclient.jar;junit-4.4.jar;myapp.jar MyMainClass
I need to reference many other jars that are found in a specific folder outside my application folder. Is there anyway I could state a folder name in the above command line to let java refer to the necessary jars from that folder.
Thanks
With java6, you can use a wildcard in classpath entries, so:
java -cp "abcclient.jar;junit-4.4.jar;myapp.jar;..\lib\*" MyMainClass
should work
(There's some problems explained here though http://javahowto.blogspot.com/2006/07/jdk-6-supports-in-classpath-but-be.html)
The very simplest way to do it is with the extensions mechanism:
java -Djava.ext.dirs=lib MyMainClass
For javac, the equivalent is the -extdirs flag:
javac -extdirs lib MyMainClass.java
It's not ideal - particularly if you also want to use the normal extensions - but it can be a useful little shortcut in some cases.

Categories