Eclipse doesn't recognize 'default' keyword - java

I am trying to make a simple Interpreter for Java.
Out teacher gave us a code to modify to reach our goal, but Eclipse doesn't like the keyword 'default'
The error is "Syntax error on token "default", delete this token"
I think it depends from my JDK version of java, this is what i am using
What should i modify?
Edit: i have Java 8 installed, but Eclipse doesn't allow me to change my compilance level over 1.7

Interface Default Methods introdced only in Java 8
Download jdk 8 and use it in Eclipse
Download

This is because you are using JRE JavaSE-1.7:
This JRE isis prior to Java 8, where default keyword has been introduced for defining default implementations of interface methods.
Switch JRE version to 1.8 to fix this problem.

Basically you just can not use/define an default method, because those are available since java 8 (and your project is using java7)... so you need to move the project to java8.
Oracle can help you, check this here
http://www.oracle.com/technetwork/java/javase/8-whats-new-2157071.html
is the What's New in JDK 8 part, 3rd element in the list....
Default methods enable new functionality to be added to the interfaces of libraries and ensure binary compatibility with code written for older versions of those interfaces.

Related

BandStructure.java missing upgrading to jdk 11 from jdk 8

openjdk-8u45-b14\jdk\src\share\classes\com\sun\java\util\jar\pack\BandStructure.java
BandStructure.java is removed in jdk11, I am upgrading to jdk 11 from jdk 8 and the above file is not present in source code of jdk 11.
Please suggest any alternative
The BandStructure does still exist in Java 11, but the Java compiler + module system now makes it harder for application code to depend on JVM internal classes.
The BandStructure class still exists in the OpenJDK codebase up to Java 13 but it is gone in Java 14. Thus class is a component of the old pack200 and unpack200 utilities that were deprecated in Java 11 and removed in Java 14.
Is there a replacement for BandStructure?
Well, in the general sense ... no there isn't. The pack200 and unpack200 commands have been removed from the codebase.
There might be a replacement for your particular use of BandStructure ... but we cannot advise you about that unless you provide more details.
If you desperately wanted to keep using BandStructure in Java 11, you could consider using --add-exports etcetera as described in JEP 261. However, this is only putting off the problem until later. By Java 14, the class is completely gone.
Note that it was always a bad idea to write applications that depended on internal classes from com.sun.* packages. The documentation has always warned that such classes could be changed or removed. In this case, it has happened.
Pack200 gone
The Pack200 feature was removed in Java 14. Thus your missing class.
See JEP 367: Remove the Pack200 Tools and API.
To avoid these surprises, I suggest:
Reading the release notes for every version of Java
Compiling and testing with each non-LTS version of Java

How to validate dependencies' java version when compiling using higher version JDK?

We're using java 8 for most modules/projects, but for some of the modules, we use java 6 (customer requirements).
The developers have java 8 installed and we compile the java 6 projects using these flags:
compileJava {
sourceCompatibility = 1.6
targetCompatibility = 1.6
}
We thought we're all good until we upgraded guava from v20 to latest - 28.1-jre.
To our surprise, the build was successful but failed at runtime.
We have a workaround for building for java 6 using a specific javac found in JDK 6. See more info here. This workaround wields the error class file has wrong version 52.0, should be 50.0 in compile time. The downside is that it requires a download+config+usage of JDK 6 for developers.
Is there a way to validate the dependencies' java version at compile time when using a higher java version? (without installing lower version java) Thanks.
Setting -source and -target values to 1.6 is insufficient to ensure that the resulting output is compatible with 1.6. The program itself must not have any library API dependencies on later versions, and the -source and -target options don't do that. (GhostCat said pretty much the same thing.)
For example, in Java 8, ConcurrentHashMap added a covariant override for the keySet method that returns a new type ConcurrentHashMap.KeySetView. This type didn't exist in earlier versions of Java. However, in the class binary, the return type is encoded at the call site. Thus, even if the source code is compiled with -source 1.6 -target 1.6, the resulting class file contains a dependency on the Java 8 class library API.
The only solution to this is to ensure that only Java 1.6 compatible libraries are in the classpath at compile time. This can be done using the -Xbootclasspath option to point to a JDK 1.6 class library, or it might be simpler just to use a JDK 1.6 installation in the first place.
This applies to external libraries in addition to the JDK, as you've discovered with Guava. The Animal Sniffer project provides plugins for Ant and Maven that checks library dependencies for version problems. Offhand I don't know if there is something similar for Gradle. There might be a way to get Animal Sniffer to work with Gradle, but I have no experience with doing that.
Is there a way to validate the dependencies' java version at compile time when using a higher java version? (without installing lower version java).
You specify your dependencies. When you tell your built system to explicitly use some library X in version Y, then you made a very clear statement.
And you see, it is not only about the class file version number. What if some person doesn't pay attention, and compiles something with Java8 ... with Java6 target, but forgets that the code bases uses Java8-only API calls?!
In other words: you are looking in the wrong place.
The person who makes updates to the build description, and changes a library version from Y to Y+8, that person needs to carefully assess that change. For example by reading release letters.
I agree that a really clever build system could check if libraries you are using come in with a matching class file version. But as said, that is only one aspect of the problem. So instead of looking into a technical solution, I think the real answer is: don't step version numbers because you can, but because you have to. And that manual step of changing that version number, that is something that requires due diligence (on the side of the human doing it).
Thus: I think the most sane approach here is to compile the Java6 deliverables within their own specific build setup. Which you only touch after careful inspection of such details. And sure: convince your customer to move on, and give up a long dead version of Java.

Java - Write code that older version ignore

I try to write my code compatible with older versions of java, so it will work everywhere.
on the other hand there is very powerful tools in the new version - java 8, and I want use them.
So I'm forced to choose between compatibility or richest code.
And I'm wondering if by any chance I can write some methods in java 8, and somehow prevent the compiler of older version to ignore these methods, so my class is compatible "partially" with older version.
Thanks.
You can write two classes and use some toll like ant, maven or gradle to chose which file use for compiling with concrete Java version.
You can set the java compiler to compile against an older jdk (ie jdk 1.5) even if you use jdk 1.8. see javac source and target options
I think the short and easy answer is no.
See this thread: Can Java 8 code be compiled to run on Java 7 jvm?
You can use the java reflection api to check if methods exist in the jvm the code runs on. This allows you to make your code fail-safe even when a method or class is unavailable in the jvm. Doing this is very cumbersome however and I'm pretty sure it's not what your're looking for.

Will programs written against "Java 8" JDK be compatible with "Java 7" JREs?

My question is if Java JDK and JREs have to be compatible to run?
I mean: will Java applications written using JDK version 8 in future work with current JRE's?
It is possible to use cross-compilation options when compiling. Do that and it will be possible to compile code with SDK 8 that is compatible with Java 1.1. It won't be very advanced code for 1.1, but it will run.
The short answer is No.
If you develop your application in JDK 8 and run it with JRE 7, you would get an UnsupportedClassVersionError.
This question is two part:
JDK vs JRE
forward / backward compatibility.
JRE is the acronym for Java Runtime Environment. JDK is the acronym for Java Development Kit: a set of tools which you use to develop Java programs. The JDK also contains a full JRE. In general there is no compatibility issue between the two. But you might want to take care not to use libraries which are only available in the JDK (for example code generation or the tools.jar)
Java itself is compiling to bytecode, which is forward compatible. That means you can use bytecode of any Java version and run it with any newer version. The other way around generally doesn't work and is checked by using the class file version ("java.lang.UnsupportedClassVersionError: Test : Unsupported major.minor version 51.0").
Then there are Java libraries, including the core libraries. So far there was never anything removed from them, so they are forward compatible. This is probably going to change with Java 9 where a very small usually unused library functions are removed.
Regarding to backwards compatibility, this is possible by setting the Java compiler to produce Bytecode of an older version. Up until Java 8, the compiler was always able to produce bytecode of the last two major versions as well. However, you might successfully compile a Java 8 source to Java 6, but not be able to run it. That is the case when you use libraries that are only available on a never Java. For such cases there is for example the maven animalsniffer plugin which will verify that when you compile against an older version, you actually only use libraries existing in said version.

ActionBarSherlock has a lot of errors

This is my first time using an external library, and I'm a bit nervous about removing all of the errors. The library when added gives me the a whole bunch of errors with the same fix "Remove #Override annotation".
The fix for this is to increase my JDK to 1.6: ActionBarSherlock library is full of errors after being imported
But now I'm worried about my other apps... Did I compile them the wrong way? Also, I have a JDK of 1.7 available, should I use that instead of 1.6? Sorry, I really don't know the difference.
this very common issue as...
This is most likely because you are switching between Java 1.5 and Java 1.6. In 1.5 you couldn't mark interface implementations with #Override, but you can in 1.6.
#Override annotation error (android prefs)
Bug with Override annotations in Eclipse
'Must Override a Superclass Method' Errors after importing a project into Eclipse
Why do I get "must override a superclass method" with #Override?
Also, I have a JDK of 1.7 available, should I use that instead of 1.6?
Java 7 language features with Android
Does Android plan to support Java7?
How does Android's Java version relate to a Java SE version?

Categories

Resources