-3

Program is compiled with JDK8. I want to run the code with JDK11(without recompilation of the code with JDK11).

Problem is that the program use classes from JDK8 tools.jar(classes that are now in the jdk.compiler module in jdk11,the tools.jar is added to the classpath). In JDK11 there is not separate jar for the tools but instead of its the part of the JDK11. So there is class clashes (class is in JDK11 and also in the tools.jar that is on classpath). The classes from JDK11 has precedence before classes from tools.jar. Is there any possibility to tell the JVM that it should use classes from tools.jar instead of the classes from JDK11.

Updates As I said I can not recompile the code with JDK11. I know about Java module system introduced in Java9. What I ask is if there is possibility to tell the JVM to ignore the classes from the JDK11 module jdk.compiler and instead of that use the classes that he founds on the classpath.

HPCS
  • 1,434
  • 13
  • 22
  • 2
    _"Uses classes from tools.jar"_ is very unspecific. Could you please edit your question and say which classes in concrete are used and for what purpose? May be there are better was in Java 11 that just using those classes. – Robert Jan 31 '19 at 08:56
  • 2
    You probably want to first learn about the Java module concept introduced with Java 9. You absolutely do **not** want to have anyone use a java 8 tools.jar with a java 11 jvm. Not at all! – GhostCat Jan 31 '19 at 08:58
  • Don't worry, I know Java module concept very well. I have specific usecase, and I can not recompile the code to jdk11 but I need to run the code with the jdk11. I need to somehow tell the classloader to load classes from tools.jar instead of jdk.compiler module of jdk11. I am asking if is there is some easy way how to do that, that I am missing. If not than I will how to intercept the ClassLoader. – HPCS Jan 31 '19 at 09:01
  • @Robert I can not recompile the code with jdk11. – HPCS Jan 31 '19 at 09:08
  • 1
    Don't do that. This is definitely not a supported configuration. – Henry Jan 31 '19 at 09:19
  • @Henry I know. I am asking if its possible. – HPCS Jan 31 '19 at 09:21
  • 1
    Let me conclude: You have a proprietary binary only software that uses tools.jar for a purpose you won't tell and you are asking us without giving further information if you can use tools.jar from Java 8 with Java 11. I see two answers: "Who knowns?" and "Maybe". – Robert Jan 31 '19 at 09:24
  • 1
    I know for which purpose it use tools.jar. It use tools.jar, because of com.sun.tools.javac classes needed for custom compilation. The question is if there is possiblity to tell jvm to ignore the module JDK11 module jdk.compiler classes and instead of that use the classes from classpath. – HPCS Jan 31 '19 at 09:27
  • 1
    Maybe with `--patch-module`? – Flown Jan 31 '19 at 09:43
  • If these classes clash, it implies that these `com.sun.tools.javac.*` classes are still there. Why not simply use the classes JDK 11 provides? – Holger Jan 31 '19 at 09:53
  • Problem is that these classes where slightly refactored and even some classes were removed like com.sun.tools.javac.file.BaseFileObject, so its not backward compatible. – HPCS Jan 31 '19 at 09:59
  • Taking tools.jar from an old JDK and putting on the class path of JDK 11 is nuts, as is trying to patch jdk.compiler with classes from an old tools.jar. If you are using supported com.sun.* APIs then they will continue to work in JDK 11, you shouldn't need to do anything. If the issue is that you have been using undocumented classes in tools.jar then you are on your own, they may not exist in JDK 11, they may have changed significantly. Stick with supported/documents APIs and you will find upgrading much simpler. – Alan Bateman Jan 31 '19 at 10:56
  • @Alan Yes, I understand the problem with that solution.Its not my code and the code use undocumented classes, for example like BaseFileObject that was removed. Its not mean to run it like this in production, I only need to test the possibilities. – HPCS Jan 31 '19 at 12:04

1 Answers1

0

Though I really don't recommend doing that, the thing you want is possible with --limit-modules option:

java --limit-modules java.se <rest of arguments>

This will exclude all modules except java.se and you will be able to use classes from tools.jar.

Again, this is terrible advice. It's much better to make your code compatible with new system classes rather than using the ones from JDK 8.

ZhekaKozlov
  • 36,558
  • 20
  • 126
  • 155
  • at current rate of jdk version succession, it would become such an annoyance trying to make existing code compatible. – Sajuuk May 06 '19 at 12:20