简体   繁体   中英

Unable to run Java code with Apache Commons

I've written a program to read in data from a text file and do some simple calculations, then print out those calculations. That part works great.

Afterward, I added in some code to do a t-test, using the TTest class (org.apache.commons.math3.stat.inference.TTest). So, I downloaded commons-math3-3.6.jar from the Apache Commons download page and put the JAR file in the same folder as the rest of my Java code for this program.

I use the following command in Windows to compile, which works fine:

javac -cp ./commons-math3-3.6.jar ./FootballTeam.java ./Main.java

But I can't figure out how to correctly run the program. I've tried this:

java Main

which executes everything up to the t-test perfectly, and then gives the expected error:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/math3/stat/inference/TTest at Main.main(Main.java:32) Caused by: java.lang.ClassNotFoundException: org.apache.commons.math3.stat.inference.TTest at java.net.URLClassLoader.findClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) at java.lang.ClassLoader.loadClass(Unknown Source) ... 1 more

I've also tried this:

java -cp commons-math3-3.6.jar Main

which gives me this:

Error: Could not find or load main class Main

I cannot for the life of me figure out how to properly set the classpath here. Can someone provide me some assistance? Thank you!

The Main class cannot be found because the current directory ( . ) is not on your classpath.

To add it, on Windows:

java -cp ".;commons-math3-3.6.jar" Main

On *n?x:

java -cp ".:commons-math3-3.6.jar" Main

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM