3

How can I change the Projects Runtime Platform?
(standard NetBeans [8.1] Java Project [Java Application])

enter image description here

Details:

  • using Ubuntu with NetBeans 8.1
  • I have installed JDK 1.6, JDK 1.7 + JDK 1.8
  • My projects platform is Java 6.
  • My systems default Java is Java 7.
  • NetBeans-IDE was started with Java 8.

When I run the project via the IDE (Run project), the Project-Platform is used (which is Java 6).

I cannot change the Runtime Platform!
It's always: Project Platform

I tried to Manage Platforms..., but I still can't change the value of the Runtime Platform-ComboBox.

EDIT:
As I wrote in the comments, I don't want to change the Project-Platform. The project should be compiled with Java 6 but run with Java 8 (when click on Run project).

What I want is:
Project-Platform = Java 6 (used to compile),
Runtime-Platform = Java 8 (used to run).

Ben
  • 3,378
  • 30
  • 46
  • Select the libraries category and change the Java Platform. You may need to add additional JDK's on the Manage Platforms window first. – WillShackleford Jan 28 '16 at 21:54
  • I want to avoid that. This is a big project and it should be fully java 6 compatible, but when changing the projects platform (to Java > 6) and keep Src/Bin-Format to J6, this **could still lead to runtime errors**, when mistakenly using classes from java > 6 (like `java.nio.file.Files`) – Ben Jan 29 '16 at 07:52
  • Setting the java platform to 6 in with the libraries category set means that it will both compile and run with java 6. Using a class only available in 7 will result in a compile time error and even if one avoided that with reflection you would still only have access to the java 6 classes when tesing the run. This is not the same as just setting the source/binary format under the sources category where your concerns would make more sense. – WillShackleford Jan 29 '16 at 14:46
  • @WillShackleford : Yes, you are right. But i want to run my **Java 6** Project with **Java 8** from inside IDE (RUN PROJECT). When i change the Java Platform from Java 6 to Java 8 (for this project), it uses Java 8 to run my Project (but the bad sideeffect is, that i can access Java 8 classes). What I want is: Project-Platform=Java 6 (used to **compile**), Runtime-Platform=Java 8 (used to **run**). – Ben Jan 29 '16 at 15:01

3 Answers3

1

How can I change the Projects Runtime Platform?

Unfortunately you can't. As you point out, its value can only be Project Platform. Of course that invites the question "What is the point of even having that Runtime Platform field?".

I suspect that it may have been (unwisely) added to the GUI so that at some time in the future it will be possible to actually select a different JDK to run against. There is an open Bug Report for this: Bug 186747 - Can't Build and Run with Different Java Versions.

It's also worth noting that the Help documentation for the Run screen does not mention that Runtime Platform field. My own view is that the field should be removed until it serves a useful purpose.

What I want is: Project-Platform = Java 6 (used to compile), Runtime-Platform = Java 8 (used to run).

Unfortunately that can't be done, as shown by the open bug. The best you can do right now is:

  • Create platforms for JDK 6 and JDK 8 in NetBeans.
  • Set the Java Platform to JDK 1.6 on the Libraries screen, and set the Source/Binary Format to JDK 6 on the Sources screen, then build to verify that no invalid APIs are being used.
  • Switch the Java Platform to 1.8 on the Libraries screen to run your 1.6 code under 1.8.

I realize that you don't want to do that, but unfortunately there's no silver bullet in the current release of NetBeans (8.2).

An alternative approach would be to build and run using only JDK 8, but include the Animal Sniffer Maven plugin "for checking projects against the signatures of an API", to verify that your 1.6 code was not using illegal APIs. But of course that's only viable for Maven projects.

skomisa
  • 16,436
  • 7
  • 61
  • 102
0

Just went through this pain for Apache NetBeans 14. I was trying to set a remote debug for a Raspberry Pi.

It works only if the jdk match on both the local and remote versions. In my case I set them both to 17.

I would have expected that the default jdk on the local machine should have been able to be changed but I fail to determine how.

I am running Ubuntu on the local machine so maybe the platform cannot be changed (don't know). Moving on. Changed runtime platform

Grant
  • 1
-1

Please look at Netbeans Project Setup - Setting the Target JDK in a Project

You just have to change the Runtime Platform at the Libraries option, that's all.

Project Runtime Change

AJPerez
  • 3,435
  • 10
  • 61
  • 91