简体   繁体   中英

how to change compiler compliance level programmatically?

Hi I am using eclipse 3.2 and i want to change my compiler compliance level from 1.4 to 1.5(ie 5.0) or higher..using java program?? not manually..means my java program will automatically do it ...can anyone tell me how to do that??

I may be interpreting this wrong. So I apologize if I did.

To me it sounds like are trying to make your app only compile with 1.5 or higher, right? In that case you can use maven compiler plug-in to have it compiled only with jdk 1.5 and also set your maven repository accordingly.

Here are the steps:

  1. First install maven on your pc or server whatever applies.This has a step by step instruction.
  2. Then create a pom.xml file yourself or use mvn create artifact to have it created for you.This link maven.apache.org/guides/getting-started/… will guide you through that process.
  3. Add the following to your pom.xml if it's not there already to add the compiler compliance.You will notice on that page about the maven compiler plugin.
  4. Run mvn install.

This seems self-referencing.. how can you choose to compile something in a way that is actually declared inside what you want to compile itself? When javac starts to compile it should already know what compliance level to use..

I think you can do it by using JavaCompiler interface (apidoc here ) but you'll have to workout something that is not so simple..

Are you really asking "how can I compile my program so it can run on both 1.4 and 1.5?"

The -target 1.4 option to the compiler will produce .class files that can be loaded by a 1.4 JVM. However, that is generally not enough. You will also need to make sure you do not use any classes, or call any methods, that are node defined in the 1.4 API.

The -source 1.4 option won't help. It will disable generics and assert as a keyword, but it won't stop you from using new APIs.

The best approach is to build with a 1.4 JDK.

Sorry I don't get the questions...

What you are trying to achieve is to change a configuration value in your IDE.

In Eclipse you can set a default compliance level for all projects. If theres a project which needs other settings, you can configure this project specific.

All settings you make are saved in your project folder in a subfolder called .settings. If you are using eclipse, try the "Navigator View" to have a look at it.

The compliance level is stored in a file called "org.eclipse.jdt.core.prefs":

org.eclipse.jdt.core.compiler.compliance=1.4

It doesn't make any sense, that your program has an dependency to your IDE changing any values...

If you want to change the compliance level, you have to do this just once. Even if you move your project in another workspace, etc. Eclipse will recognize your settings.

So where is the need to automate a simple task (just 3 clicks...) you just have to do once?

查看工作区中每个项目的根目录中的“ .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