0

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?

Neuron
  • 5,141
  • 5
  • 38
  • 59
grepgrok
  • 130
  • 10
  • @RyanLeach I have seen this and it helped a bit with downloading vecmath.jar but didn't solve the location problem. The main problem it has is that the answer is old. Since then, Java has updated and rearranged (almost thrown out) extensions. It is unfortunate that most answers to this problem link to the answer you showed when it is incompatible with recent versions of Java – grepgrok Aug 27 '21 at 01:22
  • 1
    Fair enough, as a learning exercise I guess understanding the locations and classpaths are important, but I really must stress that 99% of projects would be using build tooling such as maven etc, which are cross platform, and are answered below the accepted answer in the linked question. – Ryan Leach Aug 27 '21 at 01:58

2 Answers2

0

The jar file can go where you want, moving it to your project's lib folder is good. The real issue is you need your classpath to point to it.

Here is a full explanation.

If you are running from the command line you don't need to set the classpath variable, you can provide it in the java command. It would be something like this:

java -cp lib/vecmath.jar Example

This assumes that the program you are working on has been compiled into a class file named Example.class. If you main method is in a package you will need to fully qualify the classname so it might look like:

java -cp lib/vecmath.jar com.demo.Example

You can list multiple jar files on the classpath, separated by a colon (:).

You can also ask for help in the command line by invoking:

java -h
Neuron
  • 5,141
  • 5
  • 38
  • 59
Christopher
  • 411
  • 3
  • 7
0

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

grepgrok
  • 130
  • 10
  • Under normal circumstances, you should avoid setting or using `CLASSPATH`, but instead explicitly specify the classpath when calling `javac` or `java`. – Mark Rotteveel Aug 27 '21 at 12:57