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.
Related
I want to create a executable jar file of a small game that i wrote in java and make it playable in any machine with simple double click like exe file. My question is do you need to install java runtime first in order for executable jar file to work or can it work on a machine without any java installed as well?
No it can't. However, clients do not require the JDK, the JRE would do.
You can make your jar declare its own main class and have users double click it normally like an exe file or create a bat file in Windows.
It requires a JRE instance to be installed on the host machine. This is because JAR files are actually executed via a command line like (in Windows):
java -jar ...
This means that somewhere on your system the java.exe executable should be reachable, either by including its folder in the PATH (Windows) or replacing java with its full path.
Also, most likely you will need to have all the runtime Java libraries to be hosted on the system, as the JAR file containing the application you want to run is not supposed to contain all the Java API libraries. They are also part of the JRE package.
My explanation is tied to Windows for the sake of examples, but it can be extended to any OS.
Do you need to install java runtime first in order for executable jar file to work?
Yes, of course. To run Java .jar files first you need to have installed at least the JRE (run time environment). The JDK (development kit) is a superset of the JRE and will also work for running .jar files.
Can it work on a machine without any java installed as well?
No, as mentioned above, at the bare minimum the JRE must be installed.
You have to have a Java runtime environment (JRE) available on the machine unless you use a tool that performs ahead-of-time compilation (AOT, which is contrast to the usual Just-In-Time). Such tools are available (such as Excelsior JET), but they have a number of downsides, including cost and the fact that a precompiled Java application is a regular executable and will only run on one operating system. I've seen some installers that will detect whether a JRE is installed and launch the Java installer for the user if not.
Yes ! of course, JRE is required and it is not compulsory for JDK to be installed. Since, the main class is defined in JRE for .jar files, it is necessary to have JRE on your machine. I tried with Windows OS.
Actually you can bundle JRE within your exe file with several java .exe wrappers.
Here are few of them JSmooth, Launch4j, Jar2Exe.
I have a Windows application based on Java, that I should like to install with Java bundled. The installation framework is NSIS. The application executable should be guaranteed to invoke the bundled Java, so there's no clash with other Javas installed in the system.
What's the best way to achieve my goal? I haven't tried to solve this kind of problem before, and have little experience with Java, so I don't know which solutions are out there. I think I'd prefer Java to be embedded in the application's executable, if feasible, otherwise I guess Java could be installed along with it (with the executable pointing to said Java).
Edit:
This project already generates an executable (.exe), via NSIS. The executable will by default use the system Java, but apparently it'll prefer a JRE in the same directory (i.e. bundled) if present.
Edit 2:
The scenario that made me want to bundle Java with this application was that I received an executable for it built with 32-bit Java, which failed (silently) on my system which has 64-bit Java.
Are you absolutely sure you don't want to use the computer JRE? In most cases it's preferable.
You can see here (and the included link) some examples with installers that check JRE number and install it (globally) if necessary.
If you really prefer to include your own JRE in the installer and always use it - what prevents you from doing it? It's just a matter of your main program point having some way of konwing the JRE location and forcing to use it. That depends on how you pack/invoke your Java program. Normally, it would be feasible, perhaps with a simple .bat file - perhaps to be created at installation time.
The solution we used(A few years ago, but still I think the best way).
Find a program which can generate an exe file from a jar file(Google is your friend, I can't remember the name). Note that this .exe file will still need a jre. It is just a smart way to make an exe which contain your .jar file, and which start the java process automatic. (This also allows you to set a custom icon on your .exe file).
The java sdk which you use to develop/compile your java application, also contains a folder called jre, which contain a copy of the jre.
Include this folder with the installer, so the jre folder is located in the same folder as the .exe file. Then the .exe file will use the included jre, and the jre will not be installed on the computer, so you prevent any problems.
Well one extremely simple solution that works actually quite nice if you don't have to get an executable, is just using a simple Windows Batch file that starts the jar and having a shortcut to it so you get your preferred icon on it. For the average user there's no real difference - after all the default is to suppress known extensions on Windows (horrible default but well) - and for people who do know what an exe is, the solution should be quite apparent anyways.
That way you can also easily start the included java or better yet offer both versions and just change a variable in an ini file, which really is much nicer - nobody wants or needs X different JRE versions that are outdated and are nothing more than security problems. Still can't harm to offer a version that includes the JRE for people without a java install to make it as simple as possible for them.
One example for this behavior would be weka..
launch4j seems to offer to bundle an embedded JRE.
Can I include the rt.jar in my executable jar file and double click to run it without installing java on the machine first ? I hope it to use that rt.jar in my jar to start it self, possible ? If not, any other way ?
No, you need a java virtual machine. rt.jar is also interpreted by the virtual machine and is just the java class library.
If you're looking to turn the code native you might consider gcj which can convert java code to machine code and wrap it up in an exe as per gcc. However, I'm not sure what version of java gcj supports - I've read somewhere it isn't very recent.
It looks like Launch4j can include a bundled JRE, so you might consider this.
Java needs a Java virtual machine (JVM/JRE whatever you want to call it) to run java applications. However, I'm not so sure you need Java to be installed, rather it just be present.
You could provide this alongside your JAR (with a batch file to run the JAR files - as there won't be associations if it's not installed) but it would seriously expand the size of your project.
There are licensing issues to address too.
The file rt.jar has Java classes in it (in the form of Java bytecode). In order to interpret the classes, you will need to have Java installed.
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
This question already has answers here:
How can I get the latest JRE / JDK as a zip file rather than EXE or MSI installer? [closed]
(30 answers)
Closed 8 years ago.
I have seen many products bundeled with JDK, I wonder if there is a way where one can install JDK by simply unzipping contents to a directory, so there is no icon created in add/remove programs, no registry entries etc.
Also in this case:
How can we configure Java plugin for browsers?
And how can we configure settings as seen via Control Panel entry for Java?
According to this, I created a batch script to build jdk archives automatically.
The essential parts of the link are:
Create working JDK directory ("C:\JDK" in this case)
Download latest version of JDK from oracle (for example "jdk-7u7-windows-i586.exe")
Download and install 7-zip (or download 7-zip portable version if you are not administrator)
With 7-zip extract all the files from "jdk-[6-7]u?-windows-i586.exe" in directory "C:\JDK"
In command shell (cmd.exe) do the following:
change directory to directory C:\JDK.rsrc\JAVA_CAB10
execute command: extrac32 111
Unpack C:\JDK.rsrc\JAVA_CAB10\tools.zip with 7-zip
In command shell (cmd.exe) do the following:
change directory to C:\JDK.rsrc\JAVA_CAB10\tools\
execute command: for /r %x in (*.pack) do .\bin\unpack200 -r "%x" "%~dx%~px%~nx.jar" (this will convert all pack files into jar)
Copy whole directory and all subdir of c:\JDK.rsrc\JAVA_CAB10\tools" where you want your JDK to be and setup manually JAVA_HOME and PATH to point to your JDK dir and its BIN subdir.
Yes, you can create a zipped up JDK, unzip it on the target machine and run java, javac, etc. from that directory to your heart's content.
The easiest way to create such a zip file is to install the JDK on a machine and then zip up the contents of the JDK directory. We do this in certain circumstances where we need to control exactly what configuration of Java will be used in our deployments. In that case, our scripts just point JAVA_HOME (or the equivalent) to our internally bundled JDK instead of relying on a sysadmin to install exactly what we need prior to arrival.
In terms of integrating with the browsers, that can be a bit more problematic. The short answer is no, you can't integrate directly with the browser without some sort of installer.
You could use SysInternals RegMon and FileMon (now owned and dist by MS) to see exactly what is modified by the Java installer. I believe there will be a number of reg entries that you will want to create. Products like WISE installer, for example, also use this sort of approach under the hood to repackage product installations (e.g. as MSI).
Please be careful since there are also some dynamic decisions made by the installer which may affect what gets installed (e.g. on XP v.s. W2K3 server). I was bitten by this once regarding installed codepages. I do not recall the precise details, but the effect was that a codepage file was missing in my embedded JRE + JDK (legally redistributable portion only). This caused a very bizarre and seemingly nonsensical runtime error in my code. It goes without saying that the same applies to Server v.s. client JVM DLLs.
Really, no, at least if you want to use it from Windows and not from, say, cygwin. Windows depends too much on the registry; you could simulate the registry updates necessary, but software that moves the files to the right place and updates the registry is called "an installer"; you'd just be reinventing the installer.
I believe this at least used to be feasible to a limited extent with earlier versions of Java - I don't know if it still is.
Most of the registry entries are used for things like browser plugins, as you mentioned in the question. If you just want to be able to run Java (e.g. from a batch file), that's one thing - but really installing it is a different matter. Even if you're just wanting to run it, you'll need to be careful to always explicitly use the one you intend, rather than using the installed one accidentally for part of your application.
What's your actual use case? Do you actually need a browser plugin? What aspect of the configuration are you interested in? A lot of the control panel configuration is to do with updates and browser integration. Many other aspects can be controlled using command line options to set specific system properties.
If you just want to provide a JVM with your application is fine, but more than that I would not recommend.
If you just want to have the JDK (JRE) files, you can run the installer within sandboxie. Once installed in the sandbox, just copy the files from c:\sandbox and you are done.
I'm using this to compile and run legacy java applications which cannot be migrated easily to a newer version of java. I can point Eclispe to this JDKs and tell it to be compliant to Java 1.3.
Just down load the Windows server version of Java from the Oracle downloads page. Setup JAVA_HOME and PATH variables on your own.