• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to compile and run a java program with different java versions using Eclipse Mars.1

 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm using eclipse Mars.1 IDE. I want to compile a java program with different versions of Java. Like if I write a program in which I have an interface having default method which was introduced in 1.8 like below

When I right click on project -> Properties -> Click on Java Compiler ( Left side menu ) -> unchecked Use compliance from execution environment 'JavaSE-1.8' on JavaBuildPath and next to the Menu1. Compiler compliance level: I have drop down menu of Java version 1.3 to 1.8.

When I unchecked Use Default Compliance Settings I get following option enabled.
Menu2. Generated .class files compatibility: CLDC 1.1 and Java versions 1.1 to 1.8
Menu3. Source Compatibility: Java version 1.3 to 1.8

What I have to choose if I want to compile and run the program which has above defined interface in it. I want to know practically that only 1.8 supports this default and If I use version 1.7 or earlier then it will give compile time error. Do I have to have JDK of the version whom I want to compile and run java program with ? Or I need to have only JRE of different versions like JRE 1.5, JRE 1.6.

Eclipse Mars.1 shows the names of version in drop down menu, does that mean it has already inbuilt JDK or JRE versions which it shows in above menus. ? please guide me....
 
Knute Snortum
Sheriff
Posts: 3126
79
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do I have to have JDK of the version whom I want to compile and run java program with ?

If you want to compile a program, you need a JDK. To run a program after it's compiled you need a JRE, but one is included when you download and install the JDK.
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all thank you for your reply
If you want to compile a program, you need a JDK. To run a program after it's compiled you need a JRE
yes I know that I need jdk to compile .java file so I can get .class file and JRE to run that .class file but I was asking about different JDK versions with new features as I mentioned above about default methods in interface since JDK 1.8 and later. I meant If I want to have practical experience of the changes took place in Java features, version wise then do I have to install JDK of all those versions? as StringBuilder was introduced in 1.5, correct me If I'm wrong, so to know that practically StringBuilder wasn't before 1.5 then do I need to install JDK 1.4 and set its path in eclipse mars so it will give compile time error? hope I'm clear now...
 
Knute Snortum
Sheriff
Posts: 3126
79
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I meant If I want to have practical experience of the changes took place in Java features, version wise then do I have to install JDK of all those versions?

Yes. Eclipse has a way to keep all those JDKs and JREs straight. Windows > Preferences > Java > Installed JREs
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ohh ok, will download few versions just to check, anyway thank you once again for such quick reply
 
Tony Docherty
Bartender
Posts: 3101
61
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ganish Patil wrote:ohh ok, will download few versions just to check, anyway thank you once again for such quick reply

You don't need to download other version of the jdk to compile for earlier versions. You just need to set the eclipse compliance setting for the version of Java you want to compile for and the generated class files will be for that version of Java. You will need to download the appropriate version of the JRE and run them using that JRE to ensure they do work correctly at that version.

Eclipse handles compiling for earlier versions of Java as does javac (the standard compiler in the jdk). With javac you use the -target <release> switch to specify which version of Java you want to compile for.
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So you mean I need to download JRE only, for an example suppose Java 1.4 (Till this version StringBuilder was not introduced. It was in 1.5). To find out that I have to select eclipse compliance 1.4, here eclipse will try to compile using compiler of version 1.4 ( which is inbuilt in eclipse ) so will get error right ?.

Another for default method in interface (which was introduced in 1.8) so I have to select compliance level 1.8 and eclipse will compile it with compiler of version 1.8 (inbuilt in eclipse ) and then I must have JRE 1.8 installed on my system and set that path of JRE in eclipse and run , hope I'm on correct path ?

I know, I'm bit annoying
Tony Docherty wrote:
With javac you use the -target <release> switch to specify which version of Java you want to compile for.

Ok to compile program Test.java through cmd which resides on c:\Test.java and I have set PATH of JDK 1.8 in System environment variables on Windows 7
c:\javac -target 1.8 Test.java This compiled successfully but how to run it ? I tried to run like c:\java -target 1.8 Test but gave message Unrecognized option: -target
and suppose I want to compile same Test.java with 1.4 using command prompt (Not eclipse) then now here I need JDK 1.4 so I can have compiler of version 1.4 and to compile that I have to type
c:\javac -target 1.4 Test.java am I correct ?
 
Tony Docherty
Bartender
Posts: 3101
61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm really not sure what you are trying to achieve, if you explain in detail what problem you are trying to solve (without referring to compilers or run times, ie a general description of the problem rather than your attempted solution) then it will help us give better advice.

-target is compiler switch and not a runtime switch.

If you want to run code that is compiled for a specific version of Java you need to have a runtime for that version of Java or a later version. So you can compile for version 1.4 and run that code using a JRE version 1.4 or 1.5 or 1.6 or 1.7 or 1.8 etc however if you are compiling for version 1.4 because that is what your clients are using then you really should get a version 1.4 JRE to make sure your code does work as intended on ver 1.4. Remember each time a new version of Java comes out there can be several types of changes such as language changes (eg introduction of generics), library changes (eg addition of new classes, methods etc) and bug fixes, not to mention more subtle things like changes to the compiler, optimizations, run-time etc.
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Docherty wrote:
If you want to run code that is compiled for a specific version of Java you need to have a runtime for that version of Java or a later version. So you can compile for version 1.4 and run that code using a JRE version 1.4 or 1.5 or 1.6 or 1.7 or 1.8
This is what I was looking for. Also how to compile a java program using JDK 1.4 and JDK 1.8 compiler through command prompt ( Not using any IDE).
 
Tony Docherty
Bartender
Posts: 3101
61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You compiled from the command line in an earlier post so is your problem solved now?
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes solved thank you
 
Tony Docherty
Bartender
Posts: 3101
61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My pleasure.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!