Specifying Maven Directory when Running Java.exe - java

I'm a newbie to Java (just two days) and am trying to get an HTTP server running using Spark. I created a new Maven project in IntelliJ and have a Hello World response coming back from Spark when running it in IntelliJ. As a learning exercise, I would like to get the app running from the command line using Java.exe. When I run java Main, I get the following error:
Exception in thread "main" java.lang.BootstrapMethodError:
java.lang.NoClassDef oundError: spark/Request
at Main.main(Main.java:5) Caused by: java.lang.NoClassDefFoundError: spark/Request
... 1 more Caused by: java.lang.ClassNotFoundException: spark.Request
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
Looks like it can't find the Spark .jar files. There's a command line option -cp to specify paths to dependencies. After looking up where Maven keeps these files, I tried running:
java -cp .;C:\Users\jbuschmann.m2\repository Main
Still same error. How do I specify the path to the Maven dependencies?

Instead of giving the repository in the -classpath, try adding the location of your spark.jar.
java -cp \path\to\spark.jar Main

Related

OpenCV java project runs in Eclipse but not in Command line

I downloaded OpenCV for Java and added the jar and libraries to the project in Eclipse and it works fine.
But when I try to run the .class file where my main(String args[]) is, it throws
> java MainActivity 1 5 8
Exception in thread "main" java.lang.NoClassDefFoundError: org/opencv/core/Core
at HoleFiller.<init>(HoleFiller.java:29)
at MainActivity._fixHole(MainActivity.java:100)
at MainActivity._getImageNames(MainActivity.java:92)
at MainActivity._authArgTypes(MainActivity.java:66)
at MainActivity.main(MainActivity.java:17)
Caused by: java.lang.ClassNotFoundException: org.opencv.core.Core
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)
... 5 more
It crashes on this line
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
Is there any way to include the .jar file in the project so it will run from the command line?
Thanks in advance
When you run within Eclipse, then the IDE will automatically handle the dependencies, but when you run from the commadnd line, then you need you to handle it your self.
Follow these commands to do so:
When compiling:
javac -cp path/to/opencv.jar YourProgram.java
When running:
java -cp path/to/opencv.jar:. -Djava.library.path=path/to/opencv.dll
YourProgram
And you can add the .dll path to the system path, then it will automatically get access to it.

javamail class not found while trying to run on RHEL

I was asked to configure and use javamail on a RHEL 7 server.
I untar'ed a javamail directory they had here which contains the following files:
activation.jar
cacerts
config.properties
InstallCert.class
InstallCert$SavingTrustManager.class
mail.jar
SimpleSSLMail.class
Also, I have created a /etc/profile.d/java.sh where I have the following:
JAVA_HOME=/usr/java/jre1.6.0_45
export PATH=$JAVA_HOME/bin:/usr/bin:/bin:/usr/sbin:$PATH
export CLASSPATH=$CLASSPATH:/usr/share/javamail/activation.jar:/usr/share/mail.jar:.
cacerts is a keystore which holds gmail's cert to sendmail. When I try to send mail using the following:
java -Djavax.net.ssl.trustStore="cacerts" -Djavax.net.ssl.trustStorePassword="somepassword" SimpleSSLMail "SomeHeader" "Testing..." "some#mail.com"
It fails with message:
Exception in thread "main" java.lang.NoClassDefFoundError:
SimpleSSLMail Caused by: java.lang.ClassNotFoundException:
SimpleSSLMail
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
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) Could not find the main class: SimpleSSLMail. Program will exit
I have reviewed many posts saying the problem is java not finding mail and activation jars, but I can't handle correctly the CLASSPATH to work with those files. I also tried using -cp flag like this: java -cp mail.jar -cp activation.jar -Djavax.net.ssl.trustStore="cacerts" .....
It was useless, I get the same error.
What am I missing or how do I get it to work?
I have java jre only; do I need to compile it using javac?
My goal is to make it work in order to create an alias where the user can type only sendmail "Message" "mail.com" or similar
UPDATE
I was having a typo in classpath where instead of using /usr/share/javamail/mail.jar I was using /usr/share/mail.jar.
Now, when creating the alias it works fine in javamail directory but if I try to run it outside the directoy, java complains that it doesn't find config.properties. I believe it is searching for it as ./config_properties.
I don't have the source code, so, is there a way to override the config.properties location with the command line?

running the .jar file of jalview

I am trying to get Jalview [1] running on my windows 7 system. The standard installation works without error but when starting (the .exe file) it I get "Windows error 2 occured while loading the Java VM". As this error is quite general I tried then
C:\Program Files (x86)\Jalview>java -jar jalview.jar
which leads to:
Java version: 1.8.0_121
x86 Windows 7 6.1
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/log4j/Layo
ut
at jalview.bin.Jalview.doMain(Jalview.java:210)
at jalview.bin.Jalview.main(Jalview.java:180)
Caused by: java.lang.ClassNotFoundException: org.apache.log4j.Layout
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)
... 2 more
any ideas what is going wrong?
Thx
Morpheus
[1] http://www.jalview.org/Download
This error "java.lang.NoClassDefFoundError" occurs when there is a class missing in your respective packages.
I suggest please check if the class "Layout" is present at path : org/apache/log4j
If not then its correct that you have some missing files/libraries of log4j in the classpath.
Please vote and mark the solution if useful.
Thanks!
First check that in C:\Program Files (x86)\Jalview\lib the library min-jabaws-client-*.jar exists.
To run it from command line
cd C:\Program Files (x86)\Jalview
java -cp jalview.jar;lib/* jalview.bin.Jalview

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

Running Java Application NoClassDefFoundError

When I run my program from terminal by executing the command java rmiserver.LightBulbServer.class I'm getting NoClassDefFoundError :
Exception in thread "main" java.lang.NoClassDefFoundError: rmiserver/LightBulbSe
rver/class
Caused by: java.lang.ClassNotFoundException: rmiserver.LightBulbServer.class
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
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)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
Could not find the main class: rmiserver.LightBulbServer.class. Program will ex
it.
You are most likely running the application like this:
java rmiserver.LightBulbServer.class
You shouldn't add the .class extension when running your program; use only the name of the class:
java rmiserver.LightBulbServer
You're running the command:
java rmiserver.LightBulbServer.class
when you should be running
java rmiserver.LightBulbServer
You need to set some variables before Java can do anything on a system. This is why some people create batch files to get their Java programs running without modifying system variables.
But this usually happens because you didn't set the CLASSPATH.
For example, here is something that may or may not work:
set JAVA_HOME=C:\jdk1.5.0_06
set PATH=C:\jdk1.5.0_06\bin;C:\Windows;C:\Windows\System32
set CLASSPATH=.
java -jar myprogram.jar
Or try this command:
java -jar myprogram.jar -classpath .

Categories