Modify src.zip file in jdk folder - java

Can I modify src.zip(it contains java classes predefined ) so that i
first unzip and then add my personal package and zip and replace the src.zip with this
new src.zip i modified.
so that i can import them just like any other classes.

Check out the Java tutorial on the CLASSPATH. That provides an extensible means of adding libraries for Java usage which doesn't impact the original install, and can be segregated between running processes.
The CLASSPATH variable is one way to tell applications, including the
JDK tools, where to look for user classes. (Classes that are part of
the JRE, JDK platform, and extensions should be defined through other
means, such as the bootstrap class path or the extensions directory.)
The preferred way to specify the class path is by using the -cp
command line switch. This allows the CLASSPATH to be set individually
for each application without affecting other applications
You'll rarely have to touch the JDK/JRE install, and I would strongly recommend against it. By using mechanisms such as the above, each app can specify its own libs, and you can swap between variants of the JDK/JRE without having to ensure each deployment is modified.

The src.zip contains the source files not the class files and it won't be part of the built in classpath either.
If you have to import your classes, you have to keep you other dependent jars in the classpath.

What you want is set the classpath.
Of cause, you can add your classes to the rt.jar file, but I highly recommend not to do so.
How to set the classpath

src.zip folder will not be included in java classpath, yes you can add your own classes in it. But in order to add it your classpath ,it needs to be compiled and the resulting class files you can add use.
But its not recommended to modify jdk source, unless you know exactly what you are trying to do.

Related

Are Tomcat jars meant to be added to the classpath?

Since jars like servlet.jar are usually not downloaded on their own, but rather come part of tomcat/lib folder, should I just add an entry to them in the classpath? Is that the common practice?
I use Ubuntu.
You only need to reference them yourself when you want to compile servlet classes. How to do that depends in turn on the tools used for compilation.
If you're using plain javac, then you could reference them in %CLASSPATH%. But even then, that's considered a poor practice since that would potentially pollute the default classpath of all other Java compilations/applications. Rather write a shell file which sets the classpath right on the current execution environment by utilizing the -cp attribute of javac command.
If you're using a bit decent IDE like Eclipse/Netbeans, then you should just integrate the server in the IDE and associate the project with it. The IDE will then take care about setting the buildpath right. You don't need to set any environment variables then.
You do not need to reference them when you want to run them. The servletcontainer will take care about it by itself.
See also:
How do I import Servlet API in Eclipse?
If you are running a web application on Tomcat then the servlet-api.jar is in the classpath.

How does one build the java JRE from source (src.zip in JDK)?

Surprisingly enough I couldn't find the answer to this question.
I am trying to rebuild the java JRE from source. I obtain the java JRE source by extracting the src.zip file in the JDK.
After making any changes I need to make to the JRE, how do I compile the new source back into .java files (after which I can compress it into the rt.jar file).
Thanks.
You have better chances using OpenJDK (the base of Oracle/ Sun's future JDKs).
http://openjdk.java.net/
But what do you want to change actually? Maybe there is a better way...
Some of the Java sources that make up
rt.jar are generated during the build
process, from scripts and other means.
Some of the properties files are also
generated this way, and some of the
properties files are turned into Java
source that also contributes to
rt.jar. So without doing a complete
build first and populating the
'gensrc' directory, you won't have all
the sources that make up rt.jar.
Taken from:
http://www.java.net/forum/topic/jdk/java-se-snapshots-project-feedback/it-possible-just-build-rtjar
So when you say javac on all the java files inside src.zip it won't compile as the dependency graph is broken (missing generated files)
Also have a look at this: Where to get full source code for rt.jar?
If you want to change a number of class, you only need to compile those classes. You don't need to compile the whole JDK unless you intend to replace it.
If you just want to patch it, create a JAR of your changed classes and add this to the boot class path.
After revisiting the question. Javac on any of those files will allow you to rebuild them. Also you don't compile .java files into .java files they become .class files. You can write an ANT build script to handle the heavy work for you.

How do I build a single bundled JAR with all the needed classes to run a Java application?

I'm going to deploy a Java application with a custom launcher, and I need to have all the classes needed for my app in a single jar file so I don't have to deploy the entire Java SE libraries with it.
I was thinking of using some pre-existent ant tasks to create a target that recursively searches all my compiled classes files for its dependencies. After all the dependencies have been determined it would extract the needed class files from their JAR's, copy it along with my classes to an output directory and make a single jar from it.
If there's no such thing avaliable out of box, I can create one myself, but I need to know how references to other classes are stored in .class files. It could be much easier if there's some kind of java library like .NET's Mono.Cecil/System.Reflection that exposes an high level API for inspecting/manipulating Java.
Since I'm new to Java, I'm having some trouble in finding what is needed to acomplish those things. Can someone give me some direction?
Unfortunately, you cannot ship only part of Java SE - that will breach the license agreement. If you use Java SE, then all of Java SE must be available.
The simplest way to achieve this is to use an Ahead Of Time compiler. These take care of packaging only the classes you need, and adhere to the JDK license agreement by making the "unused" parts of the JDK available via optional download.
For example, Excelsior JET is a good AOT compiler and will package just the classes you need. It's not free for commercial use, although open source projects can apply for a free license grant.
Alternatively, you may simply assume that the user has the JRE already since it's installed on over 90% of desktops, and in cases where the JRE is not available, have your installer download one for the user. AdvancedInstaller has a free edition that will accomplish this.
After all the dependencies have been
determined it would extract the needed
class files from their JAR's, copy it
along with my classes to an output
directory and make a single jar from
it.
As an easy solution, if you use Eclipse IDE you use the following solution:
Under the Java project properties (right click):
Export... => Export as Runnable JAR
The exported JAR will have all its dependencies packed into it.
alt text http://www.ubuntu-pics.de/bild/97131/selection_016_mg6IDm.png
Here is one suggestion I found on the web:
<jar destfile="${build-abc}/abc.jar" duplicate="fail" index="true">
<zipfileset src="${compile-lib}/demo.jar" includes="**/*.class"/>
</jar>
This should add the dependencies to the jar (as shown with demo.jar). You still have to adapt the manifest file so that the added jars appear on the classpath.
This doesn't solve the 'Java SE' classes problem - you'll have to bundle a jre in the installer package or depend on an existing one on the target system.
You won't have to deply Java SE classes, as they already are in the customer JRE.
If your dependencies are expressed ion a common way (through Maven or Ivy) i guess it's quite easy to find an equivalent of maven uberjar task ... which will do what you want to do, but in a more simple way (as it simply repacks all jars in one big jar).

Where are the Java System Packages stored?

I want to see all the java packages. Where are the packages stored in my machine? Can anyone help. I did search in jdk folder and found awt.dll and all. But its only a few. Can i see all of them?
If you want a list of packages in the standard installation, just go to the Javadocs and look in the upper left corner.
If you want to see the .class files, they're in lib\rt.jar in the JRE directory (.jar is the same as .zip, so you can open it with anything that can open zip files).
If you want to see the source code, look in src.zip in the JDK directory. If it's not there, you probably elected not to install it when you installed the JDK.
Keep in mind that packages are represented as folders on disk, so you might be a little disappointed by what you see.
From Java 9 onwards rt.jar was removed
The class and resource files previously stored in lib/rt.jar, lib/tools.jar, lib/dt.jar, and various other internal JAR files are now stored in a more efficient format in implementation-specific files in the lib directory. The format of these files is not specified and is subject to change without notice.
The System class files can now be accessed as shown below
FileSystem fs = FileSystems.getFileSystem(URI.create("jrt:/"));
Path objClassFilePath = fs.getPath("modules", "java.base", "java/lang/Object.class");
Assuming you mean the packages that include the class libraries like java.lang.* and java.util.*, these live in the "lib" directory under wherever your Java Runtime Environment (JRE) is installed.
On Windows, it would be something like this:
C:\Program Files\Java\j2re1.4.2_12\lib
In there, you should see files like rt.jar which contains the core Java classes and charsets.jar which contains many of the extended encoding support for EBCDIC and the CJK languages.
In a parallel bin directory are the executables for Java and related utilities.
If you've installed the Java Development Kit (JDK), in the directory above where you find the libs you will probably find a src.jar file. This can be unpacked either with the jar.exe utility, or with a standard zip-style tool, and contains the Java sources to the standard class library.
Some of Java, such as the virtual machine itself, is machine-specific, and will be part of some of the DLL's or EXE's present.
You can try unzipping/unjarring rt.jar, which is usually available in $JAVA_HOME/lib/rt.jar. The jar file should include the classfiles of all the JDK, if that is what you are asking about.
Windows:
For compressed compiled java packages( Java Class Library, JCL): program files/java/jdk/jre/lib/rt.jar
For source of packages: program files/java/jdk/src.zip
we can use any unzipping software to look into them.
My JDK 1.6.0_13 has a src.zip containing all the source code. Give that a look.
As answered by #VenkataRaju i would like to put 2 more points that
we have ./bin, ./conf, ./include, ./jmods, ./legal, ./lib
we can see all the classlist in ./lib/classlist and ./lib/src.jar
in java 11
rt.jar is removed since Java 9. So, to find sources of JCL you should:
sudo find / -name java.base

Where are the class files located in JDK folder?

This may sound like a stupid question. Where are the core class files from Sun, like Button.class, located in the JDK installation folder C:\Program Files\Java\jdk1.5.0_12?
Or do the class files reside in C:\Program Files\Java\jre1.5.0_12 folder?
They are spread between several jars. It looks like Button is in jre/lib/rt.jar though.
The class files actually reside as jar files inside the Java distribution being used. Most files are in rt.jar (runtime).
Most developer machines actually have two forms of Java installed. The JDK (which you would often be using when developing and includes the compiler), and the JRE which is used by downloaded Java based applications, and often your web browser. Those are typically two independent distributions that don't know about each other. So the answer to your question is unfortunately, "depending what it is that you are running". To make things worse, the JDK may include it's own copy of the JRE...
This is one of the sources of the so-called classpath hell, because it is not always clear what you are using when you are running a java program.
If you run java from the command line, you can sometimes detect the exact version being used.
If you use Eclipse, you can pick version of the JDK you are working with.
On Both.
They are two different JVM's installations. One used to compile ( JDK stands for Java Development Kit )
and the other is the java environment which most customers machine have ( JRE stands for Java Runtime Environment )
Look on any of those two for a file named:
rt.jar
That where the core of the platform resides.
You can check out yourself for any class through reflection.
For example where can I find my String class located in the classpath?
Easy
URL url = String.class.getResource("String.class");
System.out.println(url);
There are actually contained in the JRE. Although, the JDK have her copy of the JRE so we may be tempted to say both JDK and JRE but in reality java inbuild classes are contained in the JRE.

Categories

Resources