Ian Boyd
Ian Boyd

Reputation: 257047

How to import a java standard class, from a particular java standard package, from a particular java standard jar?

Short Version

How do i do the equivalent of the hypothetical syntax:

import plugin.jar.netscape.javascript.JSObject; //import netscape.javascript.JSObject from plugin.jar

Long Version

I am trying to use a certain class in the Java Class Library:

So normally you would call:

import netscape.javascript.JSObject;

and that would be the end of it.

But Java has multiple implementations of JSObject

But there's a bug in the standard Java libraries; there's another implementation of JSObject:

In other words:

Library Package Class
plugin.jar netscape.javascript netscape.javascript.JSObject
jfxrt.jar netscape.javascript netscape.javascript.JSObject

Which means we now have two implementations of netscape.javascript.JSObject:

Normally this wouldn't be a problem. Any ("built-in") java library is allowed to export the same package and same class as other ("built-in") java library.

But Java bug

The problem is that Implementation 2 - the one in jfxrt.jar (JavaFX) has some bugs. 1, 2, 3, 4, 5, 6 (i.e. they forgot to implement a method)

So in the .java code file i'm looking at, for this one import, i need to force the use of Implementation 1 of JSObject - the one that doesn't have the bugs.

Except the following hypothetical syntax doesn't work:

import plugin.jar.netscape.javascript.JSobject

Note: Even if i knew how to exclude certain Java libraries from a project, doesn't mean that there isn't code elsewhere that requires the Implementation 2.

Research Effort

I started by deleting every copy of jfxrt.jar i could find on my PC (rip SmartSVN, Minecraft, Aqua Data Studio, SmartSVN-portable, Java 1.8), but the Java compiler kept finding the buggy version of JSObject.

I tried to View Definition of the JSObject class, and it conjures up a code file from somewhere, that definitely has the bugs:

enter image description here

I've tried using Process Monitor to try to spy on every file access to a file that contains the words:

in order to figure out where this buggy implementation of JSObject is coming from; but there is none. I get the impression that the Java runtime might not ship with hundreds of individual .jar files, but instead have them lumped into a larger distributable file.

Regardless of where it comes from:

Alternative Question

When i press F12 in Visual Studio Code to view the declaration of a built-in class, where is it getting that code from? If i can figure out where the code comes from, it might help me ask the next question on Stackoverflow.

Bonus Reading

Workaround

The only workaround i have for now is to break the system:

/*
 * There's a bug in Java. There are two implmentations of netscape.javascript.JSObject:
 * - plugin.jar/netscape.javascript.JSObject
 * - jfxrt.jar/netscape.javascript.JSObject
 * 
 * The 2nd one has a WONTFIX bug (https://stackoverflow.com/q/72818274/12597)
 * 
 * Which means we need to force the use of the implementation in plugin.jar.
 * But Java has no way to force to use of a specific class.
 * So we have no choice but to not use it.
 */
   private JSObject jsWindow;
// jsWindow = JSObject.getWindow(this);
   jsWindow = null; //i hope this feature wasn't important

Upvotes: 1

Views: 211

Answers (0)

Related Questions