javajava-3djava-21

How to fix Java 3d not working error with jdk21?


I have a Java 3d app that used to work with jdk8, but now after I installed jdk21, it no longer works, it's like this :

import com.sun.j3d.utils.picking.*;
import com.sun.j3d.utils.universe.SimpleUniverse;
import com.sun.j3d.utils.geometry.*;
import javax.media.j3d.*;
import javax.vecmath.*;
import java.awt.event.*;
import java.awt.*;

public class Java_3D_Pick extends MouseAdapter
{
  private PickCanvas pickCanvas;

  public Java_3D_Pick()
  {
    Frame frame=new Frame("Box and Sphere");
    GraphicsConfiguration config=SimpleUniverse.getPreferredConfiguration();
    Canvas3D canvas=new Canvas3D(config);
    canvas.setSize(400,400);
    SimpleUniverse universe=new SimpleUniverse(canvas);
    BranchGroup group=new BranchGroup();
    // create a color cube
    Vector3f vector=new Vector3f(-0.3f,0.0f,0.0f);
    Transform3D transform=new Transform3D();
    transform.setTranslation(vector);
    TransformGroup transformGroup=new TransformGroup(transform);
    ColorCube cube=new ColorCube(0.3);
    transformGroup.addChild(cube);
    group.addChild(transformGroup);
    //create a sphere
    Vector3f vector2=new Vector3f(+0.3f,0.0f,0.0f);
    Transform3D transform2=new Transform3D();
    transform2.setTranslation(vector2);
    TransformGroup transformGroup2=new TransformGroup(transform2);
    Appearance appearance=new Appearance();
    appearance.setPolygonAttributes(new PolygonAttributes(PolygonAttributes.POLYGON_LINE,PolygonAttributes.CULL_BACK,0.0f));
    Sphere sphere=new Sphere(0.3f,appearance);
    transformGroup2.addChild(sphere);
    group.addChild(transformGroup2);
    universe.getViewingPlatform().setNominalViewingTransform();
    universe.addBranchGraph(group);

    frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent winEvent) { System.exit(0); } });
    frame.add(canvas);
    pickCanvas=new PickCanvas(canvas,group);
    pickCanvas.setMode(PickCanvas.BOUNDS);
    canvas.addMouseListener(this);
    frame.pack();
    frame.setLocationRelativeTo(null);
    frame.setVisible(true);
  }

  public static void main(String[] args) { new Java_3D_Pick(); }

  public void mouseClicked(MouseEvent e)
  {
    pickCanvas.setShapeLocation(e);
    PickResult result=pickCanvas.pickClosest();

    if (result==null) System.out.println("Nothing picked");
    else
    {
      Primitive p=(Primitive)result.getNode(PickResult.PRIMITIVE);
      Shape3D s=(Shape3D)result.getNode(PickResult.SHAPE3D);
      if (p!=null) System.out.println(p.getClass().getName());
      else if (s!=null) System.out.println(s.getClass().getName());
      else System.out.println("null");
    }
  }
}

With jdk1.6 or 8, I need to do the following :

To run Java3D, copy the following files :

[ j3dcore-d3d.dll , j3dcore-ogl.dll , j3dcore-ogl-cg.dll , j3dcore-ogl-chk.dll ] From "C:\Program Files (x86)\Java\Java3D\1.5.2\bin" to "C:\Program Files (x86)\Java\jdk1.6.0_31\jre\bin" and "C:\Program Files (x86)\Java\jdk1.6.0_26\bin"

[ j3dcore.jar , j3dutils.jar , vecmath.jar ] From "C:\Program Files (x86)\Java\Java3D\1.5.2\lib\ext" to "C:\Program Files (x86)\Java\jdk1.6.0_26\lib\ext" and "C:\Program Files (x86)\Java\jdk1.6.0_26\jre\lib\ext"

But these steps no longer work, and some of the dirs no longer exist in jdk 21.

So, when I run it, I got the following error :

Exception in thread "J3D-Renderer-1" java.lang.IllegalAccessError: class javax.media.j3d.Win32NativeConfigTemplate3D (in unnamed module @0x7d0f9eb3) cannot access class sun.awt.Win32GraphicsConfig (in module java.desktop) because module java.desktop does not export sun.awt to unnamed module @0x7d0f9eb3
    at javax.media.j3d.Win32NativeConfigTemplate3D.getBestConfiguration(Win32NativeConfigTemplate3D.java:76)
    at javax.media.j3d.NativePipeline.getBestConfiguration(NativePipeline.java:3335)
    at javax.media.j3d.Renderer.doWork(Renderer.java:514)
    at javax.media.j3d.J3dThread.run(J3dThread.java:275)
BUILD STOPPED (total time: 1 minute 36 seconds)

I wonder if anyone knows how to make it work ?


Solution

  • After getting hints from @Slaw, and some more research, I found the answer, by adding the following [ in this order, it's now working ] :

     --add-opens java.desktop/sun.awt=ALL-UNNAMED --add-exports java.desktop/sun.awt=ALL-UNNAMED
    

    With the following warning :

    WARNING: Unknown module: ALL-UNNAMED
     specified to --add-exports
    

    "Dead horse" running again ^_^ !