How does javac locate the Java API classes? - java

The information on this page provides some good hints and general information, but nothing conclusive.
I know that javac.exe basically just launches 'sun.tools.javac.Main' from 'tools.jar' using Java. I also know that the Java API classes are stored in 'rt.jar' in 'jre/lib' under the JDK. Is this where 'javac.exe' loads the Java API classes from?
I'm wondering about this scenario in particular: suppose I installed jdk1.6.0_17, and later on I installed jdk1.6.0_25. If I run 'javac.exe' from jdk1.6.0_17, will it load the Java API classes from 'rt.jar' in 1.6.0_17 or 1.6.0_25?
The reason I want to know all of this is I'm in a situation where I know the code I'm compiling is going to run on a particular (not most recent) version of Java, and I want to compile using the same exact version of javac and the Java API to ensure compatibility.

By default, classes are compiled against the bootstrap(the runtime classes in rt.jar, internationalization classes in i18n.jar, and others) and extension classes of the platform that javac shipped with.So Yes If You run 'javac.exe' from jdk1.6.0_17, will it load the Java API classes from 'rt.jar' in 1.6.0_17 not from 1.6.0_25.
But javac also supports cross-compiling, where classes are compiled against a bootstrap and extension classes of a different Java platform implementation.
See more at : Cross Compilation if you want to use

Set the JAVA_HOME environment variable to the desired JDK. Also change your PATH environment variable to contain the desired JDK bin directory.

Related

JDK folder not available under program files [duplicate]

To tell the truth, I am quite confused on all these terms (JDK/JRE/Java SDK). I am not sure what each one does.
When I first started doing simple java examples in eclipse, I am pretty sure I only had the JRE, which I believed was the default java installer regular users use to be able to run java programs/applets on their system.
However, now in class we are using Google Appengine, and this requires the JDK which I am guessing is the same as Java SDK. After wasting some time finding out that installing the JDK meant I also had to add java/bin to the environment variables to get javac -version to work in the command prompt I find that only the JDK has javac...
How were my early java programs working without having installed the JDK and therefore not having javac? And really the main question... What is the difference between the JRE and JDK, and when do you use each one?
Thank you :)
JRE = Java Runtime Environment - what you need to run programs/software that require Java or use libraries written in Java. For example, OpenOffice requires the Java Runtime Environment
JDK/Java SDK = Java Development Kit/Java Software Development Kit - what you need to write programs that require Java or use libraries written in Java. For example, if you were to write your own word-processing tool in Java.
java comes with the JRE because it launches the VM (virtual machine). It can take in class files which are files that have been compiled using the JDK.
The JDK comes with javac because that's what you need to compile your .java files into .class files that can then run on the JRE.
Eclipse has its own built-in compiler (called ecj), which is probably the reason you could get away with not having the JDK installed to use it. It does not use javac.
Google App Engine uses the javac that comes with the JDK.
What is the difference between the JRE and JDK and when do you use each one?
JRE: Java Runtime Environment. It is used to run Java programs only. As Chris Jester -Young mentioned, Eclipse had a built in compiler. That's why you just needed JRE ( to run eclipse )
If you ship a Java program, the only thing the client need to have is this runtime environment
JDK: Java Develpment Kit, this also includes a JRE inside, but additionally have other tools for program development such as the java compiler javac among many others.
If you want to create java program you should use this.
There's no way you used the JRE to compile Java programs. javac, the Java compiler, only comes with the JDK.
You may write Java programs with whatever text editor, you don't need anything special to do this.
You need the JRE to run Java programs. The JRE includes the Java Virtual Machine, needed to run already compiled Java programs.
You need the JDK to compile Java programs. So if you are a Java developer, you may want to only install the JDK since it comes with the tools needed to compile, in addition to the Java Virtual Machine.
'Sometimes you can develop with jre'
No. Never.
You develop with the Java Development Kit. You run with the Java Runtime Environment or Engine or whatever it's called.

What is difference between JRE and JVM ? How does an applet works if there is no JDK present in a system? [duplicate]

To tell the truth, I am quite confused on all these terms (JDK/JRE/Java SDK). I am not sure what each one does.
When I first started doing simple java examples in eclipse, I am pretty sure I only had the JRE, which I believed was the default java installer regular users use to be able to run java programs/applets on their system.
However, now in class we are using Google Appengine, and this requires the JDK which I am guessing is the same as Java SDK. After wasting some time finding out that installing the JDK meant I also had to add java/bin to the environment variables to get javac -version to work in the command prompt I find that only the JDK has javac...
How were my early java programs working without having installed the JDK and therefore not having javac? And really the main question... What is the difference between the JRE and JDK, and when do you use each one?
Thank you :)
JRE = Java Runtime Environment - what you need to run programs/software that require Java or use libraries written in Java. For example, OpenOffice requires the Java Runtime Environment
JDK/Java SDK = Java Development Kit/Java Software Development Kit - what you need to write programs that require Java or use libraries written in Java. For example, if you were to write your own word-processing tool in Java.
java comes with the JRE because it launches the VM (virtual machine). It can take in class files which are files that have been compiled using the JDK.
The JDK comes with javac because that's what you need to compile your .java files into .class files that can then run on the JRE.
Eclipse has its own built-in compiler (called ecj), which is probably the reason you could get away with not having the JDK installed to use it. It does not use javac.
Google App Engine uses the javac that comes with the JDK.
What is the difference between the JRE and JDK and when do you use each one?
JRE: Java Runtime Environment. It is used to run Java programs only. As Chris Jester -Young mentioned, Eclipse had a built in compiler. That's why you just needed JRE ( to run eclipse )
If you ship a Java program, the only thing the client need to have is this runtime environment
JDK: Java Develpment Kit, this also includes a JRE inside, but additionally have other tools for program development such as the java compiler javac among many others.
If you want to create java program you should use this.
There's no way you used the JRE to compile Java programs. javac, the Java compiler, only comes with the JDK.
You may write Java programs with whatever text editor, you don't need anything special to do this.
You need the JRE to run Java programs. The JRE includes the Java Virtual Machine, needed to run already compiled Java programs.
You need the JDK to compile Java programs. So if you are a Java developer, you may want to only install the JDK since it comes with the tools needed to compile, in addition to the Java Virtual Machine.
'Sometimes you can develop with jre'
No. Never.
You develop with the Java Development Kit. You run with the Java Runtime Environment or Engine or whatever it's called.

Can I develop Java programs if I have only JRE installed?

I have JRE 1.7 installed on my system. Due to some admin issues I don't have JDK on my system. is there any way I can develop Java programs with JRE only?
No you can't develop java programs only with JRE
You will need JDK for compiling your programs.JRE provides only runtime environment,but JDK is something you will need to compile your code to make them executable by your JRE.You will need javac for compiling your code which is present in JDK.
However for resolving the issue of admin rights you are having, you can download and install eclipse which has its own built in compiler.
You need some program which compiles your sources, but that doesn't neccessarily have to be the JDK.
For example you can download Eclipse which only requires a JRE, it has it's own compiler (built-in) and you can develop with that. Eclipse does not require any admin priviliges, it's a zip file which you can download and extract.
You need to compile them in order to develop and that is why you need jdk
You can't develop any Java application since you need JDK(Java Development Kit) to compile your code. JRE(Java Runtime Environment) provides run time environment only. You can run your compiled code in JRE.
the answer is somehow "yes,but..."
java source code execution procedure is split into 2 steps :
1)compiling the source code into a byte-code,thus generating the .class file
2)interpreting the VM specific instructions from the .class file into native instructions
.java----javac---->.class----java---->native code
needless to mention that the "javac" , the compiler of the java source code is in not part of the JRE!
so in order to develop java programs without JDK you have to skip the first step and keep using exclusively the "java" tool,thus dealing directly with byte-code !
in some extent and if you're acquainted with the JVM internals,it's possible to write some lines of byte-code using hex or text editors and then running the .class file;but you will not go further then that specially when it comes to creating complex applications as this require super-human capabilities :that's a highly daunting task .
nevertheless,todays java frameworks almost all use direct byte-code manipulation and tools like the asm framework or javassit
javassist does not require the JDK and you can generate a class from scratch and then compile/run it
java -classpath .:javassist-x.x.jar test "some arguments"
(no need to compile classes)
but again,that is only for restricted use and in order to develop java applications you have to use some JDK...
finally,you have to strive to acquire full control upon what is running in you machine;recent frameworks are resource-greedy and needs a huge amount of resources(disk space,memory...) and many tools and framworks require full control,i mean administrator right : for instance i remember that once a time borland delphi refused to start and i had to give it full access rights to start...
I request you to resolve admin rights. Else I have two options for you.
1) You cannot do anything without Java Development Kit.
2)You can install a IDE to code Java which makes your work in the current situation simpler. Use Eclipse or NetBeans. I would recommend Eclipse to you anyway. You can download eclipse from this link https://www.eclipse.org/downloads/
Try downloading JDK from this http://www.oracle.com/technetwork/java/javase/overview/index-jsp-138218.html.
Hope this helps you.
JDK is needed to compile your java file into class file. And JRE will execute the generated class file and provides the output.
If you have admin problems in your m/c then you can install the JDK in some other m/c and take the class files into your system and execute them.
Eclipse includes its own compiler and doesn't depend on the JDK compiler. But indeed, the JRE is just the runtime environment and doesn't include the compiler or other development tools.
Do you need this to do you job or not? If it's your job, you should have access or they should install it. If you don't have access because it's not your job, I suggest installing the JDK at home (or on a laptop you can take with you)
Due to some admin issues I don't have JDK on my system.
Install it in your home directory or another directory you can write to. Your admins can't prevent you from installing the JDK if you have enough disk space.
JRE is not just sufficient, without compiling the code for that you need Jdk which have compiler to compile the code.

Where to check whether a package is shipped with the JRE for Java

I'm using javax.xml.soap package in my java desktop application and I would like to check whether that package (and more in general any package shipped with JDK) is available in JRE's on different platforms (win, mac, linux) for deployment.
The aim is to make sure that my application will run on target machines with JRE or check if full JDK is required. Also for linux I would like to make sure that the open-jdk jre 1.6 (not the oracle one) will be enough.
Thanks
If you wan't to find out if a class is included in the JRE you have, do the following:
Locate the rt.jar of your JRE, mine was at /usr/lib/jvm/jdk1.7.0_07/jre/lib/rt.jar
Open it with your favorite .zip viewer
Search for the wanted classes
You're done!
If you wan't to find out if the JRE you're running at contains your class, do the following:
Call Class.forName with the classname of the class you wan't to test
Put this in a try-catch-block
If you catch a ClassNotFoundException, its not there!
Note that this does not state if it is in the default library, just that is is on the classpath!
This effort on portability is very good!
I would suggest you requiring Java 7 because OpenJDK7 is the default implementation for it, so no more "oracle java is better than openjdk".
I think requiring Java 7 is better than requiring a specific Java implementation (Oracle) or "checking if it runs on openjdk 6".
Anyways I'd suggest david's points!
Ways to guess whether there's a package present in the JVM in runtime:
Check the JRE version system property (java.version)
Try to load the classes you're expecting in javax.xml.soap using reflection
In practice, I'd however rather demand certain JRE(s) to be used to run my application in my system requirements, rather than just trying to detect whether a package is available.

Dependencies of a JAR

I created a Jar with Java 6. Now I'm creating a release document and recording the dependencies. How can I find the earliest version of Java that can successfully run the Jar, and the earliest version of Java that can successfully compile the source into a Jar?
I only know a manuel solution: try it out. There are, however, two things to consider.
For which version is the code language compatible?
For which JRE will it execute?
The first you can do with your current JDK, just iterate over the -source and -target arguments which you pass to your javac compiler. This will, however, not prevent you from using classes and methods from the JDK you are using. If you do, the code will not execute for a lower JRE, if you are using classes or methods that where not present back then.
The savest way would be to install all different JDKs along and try to compile the code with each of their compilers.
If you created the jar with java 6 and did not specify a different version of output bytecode, the generated class files will require Java 6 or greater. You can experiment to see what versions of bytecode you can generate with your source with the -target command line option if you're compiling manually, if you're using eclipse or some other IDE, most have settings that control the generated bytecode version in project options or somewhere similar.
A related post about determining the bytecode versions of class files: What version of javac built my jar?

Categories