assert keyword(cannot resolve symbol assert) - java

GridBlock firstBlock =grid.getEntranceBlock(); //enter through entrance
assert(firstBlock!=null);
The above assert keyword is flaged by the IDEA and I don't understand why. If i try to compile i get an warning "Warning:(83, 25) java: as of release 1.4, 'assert' is a keyword, and may not be used as an identifier
(use -source 1.4 or higher to use 'assert' as a keyword)". As I understand you can use assert without having to import anything. Now i don't understand what I am missing. I tried download new JDK but it didn't help. I m using Intellij

Check what "language level" your project is using: File > Project Structure > Project > Project language level
You might be on a very old level for some reason. If there's no reason to be on it, just try increasing it to something more modern -- this will also give you other very useful language features.

Related

Are unnecessary semicolons syntax errors?

This morning, after doing a git refresh, IntelliJ complained that my project wasn't compiling clean any more. The culprit:
import javax.naming.Context;;
IntelliJ complains:
Error:(33, 29) java: Syntax error on token ";", delete this token
That code was pushed by a person who is not using IntelliJ, and it passed our backend build.
Question: is javac at fault, or IntelliJ? And what would be the document/spec that clarifies whether the above code should be an error or a warning?
This is IntelliJ 2019.2 CE EAP, running on MacOs.
And just to be precise: there seems to be a mismatch between the IntelliJ "editor compile", and the result of hitting the "build" action. Fun fact: we have "use eclipse compiler" in our project setup. Changing it that to use javac fixes the problem, the double ;; is just a warning then.
Unnecessary semicolons are not an error. They are considered as empty statements. And an empty statement, which does nothing, is not an error.
Cross check your code. It might also be IntelliJ at fault too. Its linting service might not be considering it as a legal statement.
The question is similar to this
Edit 1: The IntelliJ has an option in it's settings to which posts redundant semicolons as error.
According to the Java Language Specification §7.6 ; is a valid Type Declaration:
A top level type declaration declares a top level class type (§8 (Classes)) or a top level interface type (§9 (Interfaces)).
TypeDeclaration:
ClassDeclaration
InterfaceDeclaration
;
Extra ";" tokens appearing at the level of type declarations in a compilation unit have no effect on the meaning of the compilation unit. Stray semicolons are permitted in the Java programming language solely as a concession to C++ programmers who are used to placing ";" after a class declaration. They should not be used in new Java code.
If there is no setting in InteliJ that would change this behavior, then the extra semicolon should not produce an error.
Disclaimer
This does not resolve the issue, as it turns out the error occurs in relation to usage of the Eclipse compiler (as opposed to the default javac compiler).
This might still be useful to troubleshoot / highlight the inspection customization feature of IntelliJ.
You can set the level of severity for unnecessary semicolons (among many other things) in IntelliJ, in the Inspections profile.
The default setting for redundant semi-colons is "warning".
Probably yours has been set to "Error".
This should not impact on the Java compiler, as redundant semi-colons are not a syntactic error.

package com.sun.xml.internal.messaging.saaj.soap.dynamic does not exist

I am trying to compile a java file, which uses package com.sun.xml.internal.messaging.saaj.soap.dynamic, using ANT-1.9.3 but I'm receiving error
package com.sun.xml.internal.messaging.saaj.soap.dynamic does not
exist
I tried compiling both JDK7 and JDK 8 and getting the same error.
I can see the package exist inside rt.jar of both JDK. I have set JAVA_HOME also properly.
Can someone help me to resolve this issue?
Unfortunately for you, this behaviour is probably intentional and your usage is deliberately unsupported. If your code used to compile under Java6 and now doesn't under Java7 or Java8 then you've basically fallen foul of
From one release to another, these classes may be removed, or they may be moved from one package to another
http://www.oracle.com/technetwork/java/faq-sun-packages-142232.html
See also http://bugs.java.com/bugdatabase/view_bug.do?bug_id=6778491 for a technical description of a possible cause of not being able to compile a class which references a 'com.sun.*' class but which throws the same compiler error you're seeing despite the fact that the referenced class exists in rt.jar. Also for the kind of response which Oracle give to bug reports relating to it;)
Unfortunately, using undocumented, unsupported APIs often has this kind of 'bite you in the posterior' kind of effect.
Please add the line, < compilerarg line="-XDignore.symbol.file" compiler="modern"/ > for < javac > tag in your build.xml. This should solve your problem. It solved for me.
For reference you can view the forum link
http://www.icesoft.org/JForum/posts/list/19871.page#sthash.srrN9Ijk.dpbs

Syntax error, 'for each' statements are only available if source level is 1.5 or greater

Receiving the below error:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Syntax error, 'for each' statements are only available if source level is 1.5 or greater at Tuto.OneDimArrays.main(OneDimArrays.java:14)
CODE:
public class OneDimArrays {
public static void main(String[] args) {
int[] xabc = new int[5];
xabc[2]=20;
xabc[0]=50;
for(int temp:xabc){
System.out.println(temp);
}
}
}
Please help..
If you are using Eclipse IDE then right click on the project, goto properties, Java Compiler, Check Enable project specific settings and set the Compiler compliance level to greater than 1.5. I solved this issue by setting it to 1.6.
Hope it helps
The error tells you exactly what the problem is. You are using a for-each loop:
for(int temp : xabc)
{
System.out.println(temp);
}
This syntax was only added in Java 1.5, so you appear to be compiling using an earlier version. Without knowing what IDE/environment you're using, I can't tell you how to fix this, but see if you can find a way to compile your code using a more up-to-date version of java.
Note that there is a difference between the JRE (Java Runtime Environment) and the JDK (Java Development Kit). The JRE is used to run java programs on your computer, while the JDK is used to write java programs on your computer. Normally your system only warns you when the JRE is outdated (as most computer users don't write code and probably don't even have the JDK installed). Therefore, even if your JRE is up to date, you won't be able to compile the latest features unless you have the right JDK.
If you're using an old JDK, you can download the latest version here.
If you're using an up-to-date JDK, you will have to change some settings in your project in order to use it.
If for whatever reason neither of these are an option, you can emulate the for-each loop using pre-1.5 language constructs. The most basic approach is described in Pat's answer.
Compile this in a later version of java if you can.
If you are unable to use a later version of java for some reason just use a regular for loop:
for(int i = 0; i < xabc.length; i++) {
int temp = xabc[i];
System.out.println(temp);
}
Try changing compiler compilance level in eclipse:-
click Windows , go to preferences.
select java from the options ,select compiler in drop down menu.
change compiler compilance level to a value 1.5 or greater.
click on the link for reference: https://i.stack.imgur.com/79tvV.png
This is for eclipse.
window -> preferences -> java -> compiler -> configure project specific settings -> double click on created 'project name' -> uncheck 'use compliance from execution environment' -> then select 'compiler compliance level' to 1.5 or higher(best to choose the last level which is maximum and latest)
Done. now apply and close all. re run the program.

Compiling old Java source code with eclipse

I'm building pretty old Java code that uses "enum" as variable names, and I got "Enumeration cannot be resolved to a variable" error. How can I remove the errors?
<OLD JAVA CODE>
Enumeration enum = props.propertyNames();
while (enum.hasMoreElements()) {
Following this site, I tried to setup compiler compliance level to 1.3, but it doesn't work.
Check the Properties > Java Compiler settings again. You can get rid of the error caused by use of reserved keyword enum by configuring the compiler compliance settings like in this dialog (I am using Juno):
Juno dialog

JDE for Emacs for JDK 6.10

Is there a version of JDE for emacs that supports the JDK 6.10? I haven't been able to find any information on this. While it runs, every time I attempt to compile files the JDE says that it doesn't recognize my JDK version and reverts to assuming it is a Java5 version.
I've made following customizations for JDE:
'(jde-bug-debugger-host-address "127.0.0.1")
'(jde-bug-jre-home "/usr/lib/jvm/java-6-sun")
'(jde-compile-option-debug (quote ("all" (t nil nil))))
'(jde-debugger (quote ("jdb")))
'(jde-global-classpath (quote ("." "/usr/share/java/" "/usr/lib/jvm/java-6-sun/")))
'(jde-jdk-doc-url "/usr/share/doc/sun-java6-jdk/html/api/index.html")
'(jde-jdk-registry (quote (("1.5" . "/usr/lib/jvm/java-6-sun"))))
'(jde-regexp-jar-file "/usr/share/java/regexp.jar")
'(jde-sourcepath (quote (".")))
So it compiles without complaints, although I have jdk 1.6.0.07.
You can set your paths up in the configuration settings by "registering" a JDK version using M-x customize-variable and choosing jde-jdk-registry. Save that state, then do M-x customize-variable again, customize jde-jdk and pick the one you want.
That should do it; if not, give us a little more detailed information.
Yes, I've done that. The problem is when I call 'jde-compile, The message 'The JDE does not recognize JDK6.0.10 JDK. Assume JDK 1.5 Javac?" appears. Also, It doesn't look like the Java6 constructs, such as annotations, have been defined in the syntax checking or indentation rules.

Categories

Resources