Annotations are only available if source level is

2019-02-19 13:22发布

问题:

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.

回答1:

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.



回答2:

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


回答3:

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



回答4:

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.



回答5:

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