简体   繁体   English

如何将Java 1.5类与Java 1.6类链接?

[英]How do I link a Java 1.5 class with a Java 1.6 class?

Using a Java 1.5 compiler, when I try to compile a java class that depends on a class that was compiled with Java 1.6, I get this error: 使用Java 1.5编译器,当我尝试编译依赖于用Java 1.6编译的类的Java类时,出现以下错误:

in/javac Java15.java
Java15.java:5: cannot access Java16
bad class file: ./Java16.class
class file has wrong version 50.0, should be 49.0
Please remove or make sure it appears in the correct subdirectory of the classpath.
        String java16result = Java16.test();
                              ^
1 error

The reverse works (Using a Java 1.6 compiler, I can link with a Java 1.5 class.) 反之亦然(使用Java 1.6编译器,我可以链接到Java 1.5类。)

Is there any sort of workaround for this? 有什么解决方法吗?

“解决方法”根本就是不这样做-如果要使用Java 1.5进行编译,请同时使用Java 1.5编译其所有依赖项。

I do not think what you are trying to do is possible. 我认为您尝试做的事是不可能的。 Java 1.6 has features that Java 1.5 does not know about and therefore would not know how to execute them. Java 1.6具有Java 1.5不了解的功能,因此不知道如何执行它们。

Use the java 1.6 compiler and runtime. 使用Java 1.6编译器和运行时。

You could also decompile the 1.6 code and recompile it with a 1.5 compiler. 您也可以反编译1.6代码,然后使用1.5编译器重新编译。

I believe there's only one small difference between 1.5 and 1.6 class files. 我相信1.5和1.6类文件之间只有很小的区别。 You can probably change the magic bytes at the start of the .class file to 1.5 and have it work. 您可以将.cl​​ass文件开头的魔术字节更改为1.5并使其起作用。 There's no guarantee though and I wouldn't do this. 虽然不能保证,但我不会这样做。

The solution that worked for me was to use java.lang.Runtime.exec to execute a Java 1.6 program from a Java 1.5 program. 对我有用的解决方案是使用java.lang.Runtime.exec从Java 1.5程序执行Java 1.6程序。

I realize this is probably not the best solution in most cases, but in my case (where I needed to call a Java 1.6 program from an Oracle Database using Java 1.5), it worked fine, and didn't require any changes to my current database installation. 我意识到这可能不是大多数情况下的最佳解决方案,但就我而言(需要使用Java 1.5从Oracle数据库调用Java 1.6程序),它可以很好地工作,并且不需要对当前版本进行任何更改。数据库安装。

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

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