Search code examples
javavecmath

Where should vecmath.jar go in MacOS(11.5.1)


Preamble: So this all started with just trying to use javax.vecmath.Vector2d. I didn't have javax.vecmath so I spent a bit of time trying to get it, found that I needed to download Java3D.

After a long time of trying to download Java3D for Java (version 16.0.2), I eventually got it together with the vecmath.jar file landing in /Library/Java/JavaVirtualMachines/jdk-16.0.2.jdk/Contents/Home/lib/ext. This got rid of the error: package javax.vecmath does not exist error message.

Then, I got the message

<JAVA_HOME>/lib/ext exists, extensions mechanism no longer supported; Use -classpath instead.
.Error: Could not create the Java Virtual Machine.

this also wasn't letting me use any java commands in shell.

A bit of research and I concluded the solution to be moving (via Finder select and drag) j3dutils.jar, vecmath.jar, and j3dcore.jar over to lib and just deleting the lib/ext directory. I have gotten rid of the <JAVA_HOME>/lib/ext exists problem but back to error: package javax.vecmath does not exist.

I don't even know what to do know. I just want to use javax.vecmath. Am I going about this the totally wrong way? How can I get this to work?


Solution

  • Okay, I figured it out.

    How to use javax.vecmath in Mac OS(11.5.1) with Java(16.0.2)

    I am giving a description that sort of includes why I do things, skip to the TLDR at the bottom if you just want an answer.

    Step 1: Download the latest version of Java3D

    This contains vecmath, along with j3dcore and j3dutils. It will download a .zip file. Unzip the file and it will expand into a new directory with another .zip file inside, j3d-jre.zip. Unzip j3d-jre.zip and it will expand into a directory lib. Inside lib will be a subdirectory, ext, with three .jar files inside: j3dcore.jar, j3dutils.jar, and vecmath.jar. You can put these literally anywhere, just make sure you keep track of their location (I put them in ~/Library/Java/Extensions, this location is on the hard drive and will need an admin password to do anything–use

    sudo unzip /path/to/j3d-jre.zip
    

    if you are doing things in shell). You CAN put the ext directory in JAVA_HOME/lib/ but after Java 6, this will cause a problem.

    Step 2: Change CLASSPATH

    Java has no idea how to find vecmath.jar so you have to specify it.

    Option 1: Specify CLASSPATH with every shell command

    The simplest version is using

    javac -cp ".:/path/to/vecmath.jar:" MyMainProgram.java
    

    to compile and

    java -cp ".:/path/to/vecmath.jar:" MyMainProgram
    

    to run the program (you can also replace -cp with -classpath and it will do the same thing)

    This option won't ever destroy your CLASSPATH but you also have to include the -cp command every time you compile and run a program that imports javax.vecmath.

    Option 2: Specify CLASSPATH with every new terminal window

    A little more lasting than -cp, you can define CLASSPATH such that any changes will only take place in that terminal window. Use this form:

    export CLASSPATH=".:/path/to/vecmath.jar:"
    

    Now when you call

    javac MyMainProgram.java
    java MyMainProgram
    

    Java will see that CLASSPATH is .:/path/to/vecmath.jar and everything will compile and run without adding the -cp command.

    The main downside of this option is that if you update CLASSPATH again, you have to remember to add the previous CLASSPATH (which you can see at any time with echo $CLASSPATH)

    Option 3: Permanently add CLASSPATH to terminal

    Enter the following into terminal:

    open ~/.bash_profile
    

    this will open a window that may or may not have code in it. Regardless of any pre-existing code, scroll to the bottom and add

    export CLASSPATH=".:/path/to/vecmath.jar:"
    

    This option holds the CLASSPATH in all terminal windows forever or until you change it (using any method above).

    TLDR

    1. Download Java3D for macOS

    2. Unzip java3d-1_5_1-macosx.zip and open the directory it creates

    3. Unzip j3d-jre.zip and open the new directory /lib/ and the subdirectory /lib/ext/

    4. Move vecmath.jar, j3dcore.jar, and j3dmath.jar to ~/Library/Java/Extensions (this requires an admin password) or any other location

    5. Run the following line in terminal:

      open ~/.bash_profile

    6. Go to the bottom and add the following:

      export CLASSPATH="/path/to/vecmath.jar:$CLASSPATH"

    7. import javax.vecmath.* to any .java program you want