简体   繁体   中英

Annotations are only available if source level is 5.0 syntax error java

I am compiling a normal java program with @Override annotation(I downloaded a source from internet from some tutorial). Now the javac is complaing that

annotations are only available if source level is 5.0

Every question on SO or other site relates this to eclipse but I am not using eclipse. I am compiling the code from command line. How can I resolve the problem.

Here is some information that may be useful:

$java -version
 java version "1.6.0_13-b03
 (some other not so useful info)

Edit:

$javac -version
 Eclipse Java Compiler v_677_R32x, 3.2.1 release,....

Command for compiling:

javac User.java

User.java is just normal java file. No errors there for sure except this one.

Hi resolved the above issue, please follow the steps below:

  1. Right click on your project and select Properties .
  2. Go to the Java Compiler and check the enable project specific settings.
  3. Set Jdk versions according to jdk which is installed in your system. ex: 1.5, 1.6, 1.7

In project properties->Java Build Path/Libraries tab make sure you have JRE System Library 6 ennvironment.

Since you aren't building through eclipse, you will have to use the -source parameter to the compiler to tell it to use a higher language level. Interestingly it seems the default in the eclipse compiler is not 6 which the oracle compiler uses.

In command line use -source option of javac command to specify the version. Here is the official documentation of javac: http://docs.oracle.com/javase/1.4.2/docs/tooldocs/windows/javac.html

I ran into a similar problem when deploying a portlet project for Liferay 6.2. The build tool ANT always gave me following Syntax Error:

Syntax error, annotations are only available if source level is 1.5 or greater

I tried to set the used Java version and so on but all was set up correct. I solved the problem through editing the build.-username-.properties file for the

ant deploy

command. I just changed the compiler to modern and commented the default org.eclipse-compiler out:

javac.compiler=modern
#javac.compiler=org.eclipse.jdt.core.JDTCompilerAdapter

After that step my project was build and deployed successfully.

Was just searching a while to get that working so I though it maybe would be helpful for someone if I write it down on SO on one of the questions I checked regarding this problem.

Have sorted this legacy issue by the following steps:

1.Go to your project's Properties

2.On the Properties dialog choose the Java Compiler node in the left tree.

3.Finally set the Compiler compliance level to 1.5 or more as that's what for eclipse was crying :-0

4.Rebuild the project

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM