What's wrong with the Java source code? - java

In the Java source from http://download.java.net/jdk6/source/ I get a jar of size ~130mb. The jar don't attach to Eclipse, and inside it has a file called "X_X" of size ~130mb.
Does anyone know what's happening?
--update
Thanks Gerco Drie,
now, about this README file:
JDK requires a high level of
technical expertise. (...) If you are not a
technical professional in one of these
categories, this release is probably
not for you.
so, where is the already buid one for me?
I'm using Java 6 update 15.
--update
The site http://java.sun.com/j2se/1.4.2/docs/tooldocs/windows/jdkfiles.html says the src.zip is in the root jdk folder (yes, I remember it was last time I needed it), but its not..
Is my jdk a pirate version? :P

You need to run the jarfile, not extract it or link it to eclipse. The instructions are here:
http://download.java.net/jdk6/6u20/promoted/b01/docs/build/README-JRL.html
These files are jar files that need to be run, not un-jar'd, for example: java -jar filename.jar
I think BalusC is right by the way, this is not the sourcecode you want. You probably want the rt.jar sourcecode, which is in src.zip with the JDK. What you downloaded is the complete sourcecode of the JVM, hotspot compiler, etc included. Although it also includes the class libraries, there is an easier way.

The distribution you download is a self-extracting file which displays a license YOU MUST ACCEPT before the things you need are extracted.
Run it, and see if it answers your issues.

Related

How does importing libraries in Java work

I'm starting to program in Java and I can't quite understand how importing libraries works.
Suppose I need to import a library in order to be able to write a specific program. I save it in my PC.
How is someone who does not have the same library on their machine supposed to be able to run the program? This is probably a silly question, but I really am new to programming.. Thanks.
They need a java runtime (JRE) installed.
Third party libraries like apache commons, accompany your application jar.
myapp.jar
lib/apache-commons.jar
lib/ido.jar
The myapp.jar (zip format) contains a METAINF/MANIFEST.MF containing an entry
Main-Class: ...
Class-Path: lib/apache-commons.jar, lib/ido.jar
And with that myapp.jar is an executable jar.
A build tool like maven can be used to build such a distribution: take care of library versions (dependencies), preparing the manifest, generate to output folder.
"Someone who does not have the same library on their machine" is a wrong concept. A machine has to have the Java Runtime Environment (JRE) installed on it in order to run a Java program (i.e., to execute the .class file). The JRE contains all the Java libraries. Therefore, every machine running Java has every library that you can possibly use in your program.
Importing a library in your program is basically instructing the compiler to include that library in the program. You don't really save the Java program on your PC with the libraries in it. It's just a set of instructions. These instructions are given to the Java platform on any machine the program is run on. While running the program, the JRE on that machine uses the libraries as instructed in the .class file.
I hope I could make it clear to you.
In addition to the above answer, have a look at the libraries, which are found by:
right click on project
build path
configure path
libraries
You can check weather the JRE is added to your project or not
If it's not present click on add libraries and add it

7zip command line - Create executable jars

I have an executable .jar-File which works, but I want to add some files to the .jar-File with another program. My idea was to use the 7zip command line extension, but when I try to add something to the .jar-File. I use this command:
7za.exe a -t7z C:\myfiles\thejar.jar C:\filestoadd\* -r
Everytime I try to do this the CMD throws me an error and says:
Error: C:\myfiles\thejar.jar is not supported archive
Well, ok. Then my Idea was to unpack the file thejar.jar, add the files to the directory where the files from thejar.jar were extracted and create a .zip with the extension .jar. When I did this, the file thejar.jar was about 1MB smaller than before with adding files to it. I tried different compression-methods but it was always smaller. When I tried to execute the .jar, an error-message popped up and said Invalid or corrupt jarfile. I already googled my problem, but I haven't got an answer for now... Can you help me?
The simple / recommended solution is to use the jar command that is included in every Java JDK to add the extra files to the JAR.
It is also possible to create a JAR file using JarOutputStream:
How to use JarOutputStream to create a JAR file?
The trouble with using 7zip, or any other "standard" zip utility is that you might accidentally use some modern zipfile feature that the Java utilities don't understand.
You also asked (in comments) if you are permitted to copy the jar.exe and jli.dll from an Oracle JDK / JRE into your own project.
Read the license! We are not lawyers here and we can't give you proper legal advice.
My understanding is that that would be a violation of the Oracle license and copyright. Embedding a full JRE is permitted, but cherry-picking is not permitted.
Consider using OpenJDK instead. The licensing for OpenJDK is GPL based and there are no copyright-based restrictions on redistributing a vanilla distribution. Not even modifying and redistributing it ... so long as you make available source code and build instructions for any changes you make to it1.
Finally, note that this is moot in Java 11 onwards. Oracle now only distributes full JDK's. So that means that if your customer installs "Java" from Oracle, the jar command will be included.
1 - You only have to worry about trademark. Basically, the trademark license says you are not permitted to call a product "Java", etc if it deviates from the "standard" in non-permitted ways. Lookup the details for yourself.
-t7z will create a 7z archive, not a zip. You need -tzip instead.
I know this is an old post but if anyone is searching for info the following works great and makes a jar that will execute correctly, I ran across this post myself looking for info and in the end came up with the following.
For me this method was the easiest way to do what I needed, and just easier and faster then using jar, plus works great as batch file
C:\Progra~1\7-Zip\7z.exe a -tzip -mx9 -spf -r Filename.jar *

Could not find the main class (on other computers)

I finished writing a java program and am ready to export it. I made a runnable jar from Eclipse. Running the jar works just fine on my computer, but throws the "Could not find main class" error on any other computer (including my other computer that I write Java on).
Whenever I search around for other people having the problem, it's always the same answer: Check the manifest file. I'm not seeing any problem with mine (Plus, can't imagine why it'd work on my computer but not someone else's)
Manifest:
Manifest-Version: 1.0
Main-Class: my.quick.monster.QuickMonster
And I've also tried:
Manifest-Version: 1.0
Class-Path: .
Main-Class: my.quick.monster.QuickMonster
Both work for me, but not other computers.
Thinking about the things that might be going wrong, here are a few other things to check:
make sure that there are no spurious characters (tabs, spaces) at the end of the lines.
check that the main class is actually in the JAR file with the right name.
on the machine that works, try changing your current directory and seeing if it still works.
check that you are using the same version of Java on each machine. Run java -version to check.
make sure that you are running it as an executable JAR; i.e. as java -jar foo.jar not as java -cp foo.jar.
(One theory is that the JAR is working on the one machine in spite of the manifest; e.g. that it is finding the class via the classpath in your CLASSPATH environment variable or something.)
To summarize for other folks, the OP's problem turned out to be that he had compiled his code with / for Java 7, and was trying to run the JAR on older Java installations. That wasn't working because of the classfile version numbers.
You can compile your code so that it will run on an older version of Java, but you need to use the -target option when compiling, and you ought to use the -bootclasspath option to compile against an rt.jar from the oldest Java version. A typical IDE will simplify this by allowing you to specify the target build platform, but it is worthwhile understanding the technical details, for cases where you are not using an IDE.
(I'm surprised that the java command didn't mention the classfile version number in the error message ...)
Make sure the MANIFEST.MF file contains a blank line at the end. If the Main-Class definition is on the very last line of the file, some class loaders ignore it.
Do not ever use 'eclipse-jar-worked-fine-on-my-computer'. I use maven shade jar plugin which excellently build a ready to run jar with all the dependencies, specified main class, etc.
EDIT:
What is the wrong with eclipse-builded-jar is that you won't been able to build it w/o elcipse. Maven is the common tool widely used to build packages of any kind. It's automated, and means that it can be used in CI environmet, etc. And the goal of a good developer is to write code so that it can be easily moved to CI.
However, if it's not a regular task, assuming to make just once/twice, theen, maybe, 'eclipse' solutio has also some benefits. But, I answered keeping in mind some cases of my past when people build packages in GUI just because they didn't manage to do it in maven.
So, I hope there is enough arguments for maven vs eclipse, so please stop downvote :D

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.

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

Categories

Resources