• Post Reply Bookmark Topic Watch Topic
  • New Topic

command for running code on JDK8, as JDK7  RSS feed

 
Puspender Tanwar
Ranch Hand
Posts: 491
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have JDK8 on my system, but as I am preparing for OCJP7, I want to run my code on JDK7. How can I run my code on JDK7 if my system has JDK8. I am using command promt ??
 
Campbell Ritchie
Marshal
Posts: 56220
171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All Java7‑style code will run happily on JDK8, so you don't really have to do anything.

You can always install JDK7 alongside JDK8, and change the PATH with instructions like
set PATH="c:\Program Files\Java\jdk1.7.0_79\bin";%PATH% (Windows®) or
export PATH=/home/campbell/java/jdk1.7.0_79/bin:$PATH (Linux) or similar at the command line/terminal. Remember to put the new Java® folder at the start of the PATH. There are _ characters which you cannot see because of the underlining.
 
Puspender Tanwar
Ranch Hand
Posts: 491
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes all java7 codes runs happily on JDK8, but there are some new features in java8. So this could create problem for my OCJP7 exam.
Anyways thank you Campbell.
but is there any other way to run my code as JDK7 in JDK8(without installing JDK7), like any particular command in command promt. like -version, -source etc. ?
 
Paweł Baczyński
Bartender
Posts: 2061
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are new things added but I don't see any problem with learning Java 7 on JDK 8.

Except maybe for a question like
What is the output of this program?
 
Campbell Ritchie
Marshal
Posts: 56220
171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use options to create Java7 bytecode on a Java8 javac tool. Look for the javac and java tools' websites and you should be able to find those options.

But you can simply write Java7 style code on Java8.
 
Guillermo Ishi
Ranch Hand
Posts: 789
C++ Linux Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Puspender Tanwar wrote:I have JDK8 on my system, but as I am preparing for OCJP7, I want to run my code on JDK7. How can I run my code on JDK7 if my system has JDK8. I am using command promt ??

You could probably give the full path to the 7 compiler on the command line. 7 will run on 8, but some things 8 will accept are errors in 7, so good thinking.
 
Puspender Tanwar
Ranch Hand
Posts: 491
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paweł Baczyński wrote:There are new things added but I don't see any problem with learning Java 7 on JDK 8.

Except maybe for a question like
What is the output of this program?

the concept of using effectively final is introduced in java8. And JDK7 complaints compilation error is such cases
for example : local classes can only use final local fields in java7, but local classes can use final and effectively final local fields in java8
same thing your code is explaining by anonymous class
and exactly I want to check on JDK8 as JDK7
 
Puspender Tanwar
Ranch Hand
Posts: 491
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:You can use options to create Java7 bytecode on a Java8 javac tool. Look for the javac and java tools' websites and you should be able to find those options..

ok Campbell , thank you
 
Campbell Ritchie
Marshal
Posts: 56220
171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't remember waht the options are, but look for source and target. Those might be what you want.
 
Rob Spoor
Sheriff
Posts: 21117
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I recall correctly, it's as simple as -source 1.7 -target 1.7.
 
Campbell Ritchie
Marshal
Posts: 56220
171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you are correct, Rob. You might also be able to use
-source 7 -target 7

If you have a Java8 installation however, you would need to ensure you only use classes and methods available in Java7. Otherwise you can end up using things like List#sort() which are only available in Java8.
 
Puspender Tanwar
Ranch Hand
Posts: 491
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know when to give this command !!
can you please tell me
my code is stored in c:\myprograms
and my file name is Test.java
 
Puspender Tanwar
Ranch Hand
Posts: 491
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
But you need to verify that you are only using Java7 constructs. I tried it the other day and managed to slip a call to the List#sort() method past the JVM. If you are using a proper Java7 JRE, you would suffer a no such method error.

no, I didn't encounter such problem, but got a warning "bootstrap class path not set in conjunction with -source 1.7
but anyways, my code ran on java 1.7
Campbell Ritchie wrote:
javac -target 7 -source 7 Test.java
java Test

it is : javac -source 7 -target 7 Test.java
java Test

thank you Campbell
 
Campbell Ritchie
Marshal
Posts: 56220
171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome

I had the same error message about bootstrap classpath.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!