String in switch java - java

switch(title)
{
case "Return":
returnBooks();
break;
}
strings in switch are not supported in -source 1.6, use -source 7 or higher to enable strings in switch
So, I have already installed JDK7, Why compiler show this message and giving an error while on compiling?
I am using NetBeans

I hate to say it, but: read the darn error message. It tells you exactly what the problem is.
You are compiling in Java 6 mode, not compiling in Java 7 mode. If you're using javac that comes with JDK 7, as the error message says, use the -source 7 flag:
> javac -source 7 {stuff here}

Two key points from the user query:
Already installed JDK7;and
Using Netbeans
So, netbeans came with two package:
With JDK
Means when install the Netbeans 7 it automatically install JDK 7 and set JDK 7 as Default Library.
Without JDK
On the installation of NetBeans 7, it found JDK in the system, and if found it automatically configure the path of library as well, but show you information about the path and version of JDK7.
All of users try to give the correct answer of this query, but no one is in position to answer in the correct way as the user required because user using the Netbeans as well.
With continue the edit version of Hemal Pandya, one thing more is required to configure, which is that
RightClick on Project > properties > and in the categories option select > source. see the
**Hemal Pandya** edit version to look at image, the source option is available above the
Libraries option.
And
then select **Source/Binary Format** form bottom and set it to JDK 7 (= 1.7). this is the exact solution
of user's post and I am 100% sure now String in swich will work

In NetBeans, go to the properties of the project and change the java platform to java 7 in libraries

I have found a solution:
If you are using Netbeans 7.1 (even, 7.0), just you have to follow this:
Right Click on Project -> Properties -> Sources -> (On the bottom) Source/Binary Format
Just select JDK 7, and that's all, you can then use strings in switch!

I do not use NetBeans but there seems to be a compliance switch that has defaulted to 1.6. You will have to find that switch and set it to 7, as others have pointed out.
EDIT: I found I found netbeans.org/kb/docs/java/project-setup.html#projects-jdk. You seem to have done the second step of registering jdk. But maybe it is not the default? Follow the instructions to To switch the target JDK of a standard project. Looking at images it seems to be in this dialog:

Related

How can i use eclipse 2020-09 to develop java 8 projects?

I am a regular IntelliJ user, but most of my co-workers use Eclipse. A lot of them say the latest version of Eclipse "2020-09", which requires a JRE "higher than Java 8" in order to run, cannot be used to develop projects requiring JRE8.
This is pretty surprising to me, since I know it is possible to install multiple JDKs and JREs on one machine, I've done it plenty of times.
What is the typical way a person would configure Eclipse 2020-09 (already installed) to work on a project that requires a Java 8 JRE?
JRE 8 apps in eclipse workspace is possible by having the jre 8 jdk. It is true that in order to use eclipse 2020-09 you need java 11+ jdk/jre, but making projects that support jre 8 is still possible.Even I still use 2020-06 and before. Here is a method you could use to get JRE 8 implemented back in your project. Although you can change the defaults, this one will help you change the environment back to java 8 for 1 project.
Right click the library named somthing like "JRE system library[JavaSE-11]". Press properties. This is what you will see JRE System Library chooser image
Choose "Alternate JRE" then click "Installed JREs"Step Image
Press "Add" then you will see 3 options. Choose standard VM. Find your java 8 jdk directory. Usually it is something like C:\Program Files\Java\jdk1.8.0_251.
You can configure the default arguments for your jvm runtime. Then simply hit ok,select the jdk-8 library and close it. Then choose alternate jre again and in the drop-down choose jdk-1.8.0 or some name like that. This will only work if you also have java 8 installed alongside java 11 that is configured properly JDK
You can use Java 11 to start the Eclipse 2020-09 and configure a project to compile with Java 8. The problem is if you want a Eclipse plugin that uses CORBA for example. This module was removed from Java 11 earlier. In this case, I recommend to use Eclipse 2020-06.
Earlier versions of 2020-09 was working with Java 8. I had it, too. Then I updated and I got the same error message like you.
I propose to use an older one or to install JRE 11 and set the JAVA_HOME env variable to it.
The inside the IDE you can set java 8 like an JDK and develope your project with Java 8 while eclipse is running with 11.

java version in not match for eclipse

I had java version 8 previous and my eclipse worked just fine. But now I have java version 7 and I can't open eclipse on my computer. What should I do? please help.
I think there are two options:
Upgrade to Java version 8 (or newer).
If you can't do that, downgrade to a version of eclipse that supports Java 7. eclipse 4.5 (Mars) (or earlier as noted on the linked page), as of eclipse 4.6 (Neon) Java 8 is required.
I feel I should note that newer versions of Java (and eclipse) can still target Java 7 (and many earlier versions).
But now I have java version 7 and I can't open eclipse on my computer. What should I do?
Use versions that work together. When you for example look at your second screen shot, it says something like "requiredJavaVersion=1.8" or so.
In other words: you can't just come in and run eclipse with any version of java.
The other answer gives you the required details.
And a final note here: in case you tried to start eclipse with that older JVM in order to "allow" to ensure that eclipse won't allow "java 8" stuff in a project: that isn't the right way then. Instead: eclipse allows you to "define" JDKs to be used for your project. So you can easily tell eclipse: "I have a Java7 jdk sitting here, please use that for project X".
You do not need to run eclipse itself with a Java7 JVM in order to use a Java7 JDK for an eclipse project!
There will be a file under the Eclipse installation directory named as eclipse.ini.
It will launch the eclipse for that specific jdf and you can change the path to your jdk7.
Hope it helps.
It depends basically on which version of the eclipse you have.
If you have eclipse 4.6(Neon) or the latest version, then you must need a newer JDK version of Java (>=1.8 which you need to download). Or If you want to roll back to an older version of Eclipse then consider downloading eclipse 4.5(Mars).
Or If you have an eclipse version of 4.5(Mars) or older then see in eclipse.ini
-Dosgi.requiredJavaVersion = 1.8 and change it to -Dosgi.requiredJavaVersion = 1.7 which can be found in the folder containing eclipse.exe file.

Eclipse warning "when selecting 9 compliance make sure to have a compatible jre installed and activated..." only shows up in pre-existing projects

If I create a project in Eclipse from scratch, and I assign Java-9 as JRE :
And after that I go to project properties, I see the following message The system libraries from the selected release 9 will be used with '--release' compiler option using current JRE 10:
If I change an existing project of Java 8 to use Java 9, as the image shows:
And after that go to Properties - > Java Compiler, I see a different message, When selecting 9 compliance make sure to have a compatible jre installed and activated:
I don't understand why is there a different message in both scenarios, I believe that I'm doing the same, so I think that should I get the same message.
I want to switch the jdk of my existing project, so my question is: Is there any problem if I keep using the last project with that Warning? Or should I create a project from scratch in order to be sure that will work properly?
EDIT: List of Installed JREs:
Execution environment list:

How can I specify a blanket build config in NetBeans 8?

As we know, we are in transition between Java 7 and Java 8. I have both Java 7 and Java 8 JDK installed. However in the interests of keeping a hands-off approach I want to make java 7 JDK my default Java build environment.
Leaving Java 8 JDK installed for trials, testing, and migration.
However at the current time, Netbeans 8 insists that Java 8 is-the default.
What I'd like to set: Java 7 is default.
And only specify Java 8 on a per-project basis until I'm satisfied with the new build. At that point, I imagine we would change our default Java. The challenge is that there's no clear way to change a default Java configuration.
This is not helped by the fact that the "Java Platform Manager"is not part of "Options", and lives on the Tools menu / "Java Platforms. I have been able to to this successfully with IDEAJ when moving from Java 6 to Java 7. Everyone is using Netbeans on this project so we need to achieve the same end with Netbeans. To be clear, I'm comfortable using Java 8 for the Netbeans runtime. It is just project builds that I want as a specific Java 7 target environment.
According to the topic below: In NetBeans how do I change the Default JDK?, changing NETBEANS_JDKHOME will also change my netbeans running environment. So I would prefer to just set the build-target generally. Thanks in advance!
see also:
In NetBeans how do I change the Default JDK?
Netbeans v8 README: "nbjdk.active [since 5.0u1] - name of a Java platform (JDK) to use when building and running".
If I understand you question properly, each project can be assigned it's own Java Platform property, for example, right click the project in question and select "Properties"...
From the properties dialog, select "Libraries" and then select the "Java Platform" you want to use...
Personally, I find having the platforms pre-configured simpler.
If you're upgrading a project, you may also need to check the "Source/Binary Format" property in the "Sources" sections (it's own the bottom), as it will stay at the level of the old version of Java (if you downgrade, it adjusts automatically)
One solution is to change the netbeans configuration file netbeans.conf with is located in ...\NetBeans 8.0\etc.
But it does not work well...
The bypass we found is to spefify the jdk in the shortcut that launch Netbeans into
"...\NetBeans 8.0\bin\netbeans.exe" --jdkhome "C:\Program Files (x86)\Java\jdk1.7.0_60"

Cloudbees RUN#cloud select Java 1.8

I am using Jenkings on Cloudbees and I would like to change the JDK version to 1.8.
I followed the tutorial at developer.cloudbees.com/bin/view/DEV/PreInsatlledJDKs. However, I fail at step 10 -> There is no such option to choose the JDK. Where can I change this?
UPDATE: I made some screenshots to better explain my problem.
This is the jenkins configuration for the JDK. As you can see, I only have one JDK selected and it's Java 8.
However, if I view my "System Information", everything looks like Java 7. It says that "java.home" still points to "/opt/java7/jre" and "java.runtime.version" points to "1.7.0_25-b15" for example.
Not surprisingly, the compilation process of my maven project fails, because for the maven compiler plugin I specified 1.8 as source and target java version.
If you have one single JDK, then you won't see that option, cause it will pick it up by default.
You will se the JDK option with the drop down menu if you have more than 1 JDK configured in your jenkinsURL/configure.

Categories