开发者

How can I build a jar for a previous Java version?

I am trying to compile my code and run it on a different server. The problem is my JRE version is Java version "1.6.0_13" and that on the server is Java version "1.4.2".

This gives me "unrecognized class file version" exception when I try to run the jar on the server. I cannot compile my code on the server because of various dependencies I will have to set up.

So I need to know if there is a way to "use Eclipse" [this is easier than command line since it takes care of dependencies] and compile my stuff using 1.4.2 instead 1.6.0_13. Do I have to uninstall my JRE from my machine, then reinstall previous version and then compile or is there an elegant and cleaner way开发者_运维知识库?


You should install 1.4.2 along side 1.6 on your machine, and configure that eclipse project to use the 1.4.2 JDK. Other projects may be configured to use the 1.6.

Technically you can compile to 1.4.2 compatibility with JDK 1.6, but odds are you will run into library problems, so it is not generally worth doing.


You can compile for java 1.5 with the parameters

javac -source 1.5 -target 1.5 -bootclasspath "C:\Program Files (x86)\Java\jre1.5.0_21\lib\rt.jar"

Do not forget the bootclasspath, or you will have problems with functions like String.isEmpty() which don't exist in the java 1.5.


Eclipse Mars (4.5.1)


Change JDK compliance

Window > Preferences > Java > Compiler > JDK Compliance > change Compiler compliance level:

How can I build a jar for a previous Java version?

If you want to change other details, you can uncheck "Use default compliance settings".

Press OK and in the "Compiler Settings Changed" window choose:

  • Yes if you want to build all projects from the workspace:

    How can I build a jar for a previous Java version?

  • No if you want to build only one/some project(s) later

    • Select the project (click on the left side - E.g.: in the Package Explorer) > Project > Build Project

Export jar

Right click on the project (from the left side - E.g.: in the Package Explorer) > Export... > type "jar" to filter the options you have > choose what you want (probably JAR file or Runnable JAR file) > Next > add the details > Finish


You can configure the Source and Target Java version in Eclipse.

Also in command line you can compile for a certain (older) Java version using -source and -target parameters:

javac -source 1.4 -target 1.4
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜