简体   繁体   English

如果我使用的库的源和目标级别为1.7,如何将我的项目编译为java 1.5?

[英]How to compile my project to java 1.5 if the library I use has source and target level of 1.7?

I'm working on a web application which needs to be run on a legacy WebSphere server which runs on an 1.5 JVM. 我正在开发一个Web应用程序,它需要在1.5 JVM上运行的遗留WebSphere服务器上运行。

My problem is that if I set the source/target level to 1.5 in my pom file it works fine 我的问题是,如果我在我的pom文件中将源/目标级别设置为1.5,它可以正常工作

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
        <source>1.5</source>
        <target>1.5</target>
    </configuration>
</plugin>

until I add a dependency to my project: 直到我为项目添加依赖项:

<dependency>
    <groupId>com.sparkjava</groupId>
    <artifactId>spark-core</artifactId>
    <version>1.1.1</version>
</dependency>

I checked its pom file and it has 1.7 as source/target: 我检查了它的pom文件,它有1.7作为源/目标:

<configuration>
        <source>1.7</source>
        <target>1.7</target>
        <optimize>true</optimize>
        <debug>true</debug>
</configuration>

I get this error when I try to start up my app: 我尝试启动应用程序时出现此错误:

Exception in thread "main" java.lang.UnsupportedClassVersionError: Bad version number in .class file
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:621)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)
    at java.net.URLClassLoader.access$100(URLClassLoader.java:56)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:268)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
    at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)

If I set the target level in my project maven fails with another error: 如果我在我的项目中设置目标级别maven失败并出现另一个错误:

[ERROR] javac: source release 1.7 requires target release 1.7

What can I do to solve this problem? 我该怎么做才能解决这个问题?

You're using maven, and that means the spark-core dependency is being pulled from maven central as compiled bytecode in a .jar file. 你正在使用maven,这意味着spark-core依赖从maven中心被拉出来作为.jar文件中的编译字节码。

The pom for that project on github has it being compiled to Java 7 bytecode. github该项目pom将其编译为Java 7字节码。 I'm willing to bet money that's what is uploaded to maven central. 我愿意下注那些上传到maven central的钱。

The only way it would be possible for you to target Java 5 and use that library would be to clone the project, modify its pom to specify 1.5, and build your own jar. 您可以定位Java 5并使用该库的唯一方法是克隆项目,修改其pom以指定1.5,并构建自己的jar。

That said, it is highly unlikely that it will build for a ten year old version of Java given that they specify 1.7 in the pom; 也就是说,鉴于它们在pom中指定了1.7,它很可能不会构建一个已有十年历史的Java版本; they are probably using Java 1.7 features. 他们可能正在使用Java 1.7功能。

抓取代码,将其编译为1.5(如果它编译)并插入到您自己的存储库中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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