OpenCV java project runs in Eclipse but not in Command line - java

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.

Related

NoClassDefFoundError using bluetooth in java

I asked a question earlier here: NoClassDefFoundError with bluecove's javax.microedition
but got no responses.
I am trying to run some java code (Server.java) that requires bluetooth, so I installed the bluecove-2.1.0.jar file in C:\Program Files\Java.
I compile my code with javac -cp "C:\Program Files\Java\bluecove-2.1.0.jar";. Server.java and run it with java Server -cp "C:\Program Files\Java\bluecove-2.1.0.jar"
It compiles successfully, but when I run it, I get the error:
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: javax/microedition/io/Connection
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
at java.lang.Class.privateGetMethodRecursive(Unknown Source)
at java.lang.Class.getMethod0(Unknown Source)
at java.lang.Class.getMethod(Unknown Source)
at sun.launcher.LauncherHelper.validateMainClass(Unknown Source)
at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)
Caused by: java.lang.ClassNotFoundException: javax.microedition.io.Connection
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)
... 7 more
Any ideas where i've gone wrong?
EDIT: Following Minus' answer, to fix the error that followed, I also needed to use a different .jar file which I downloaded from http://snapshot.bluecove.org/distribution/download/2.1.1-SNAPSHOT/2.1.1-SNAPSHOT.63/
The problem is that the .dll is not loaded correctly.
The library should contain a .dll that will be saved on disk and loaded, maybe the program cannot save the .dll to disk or cannot read it.
Extract the .dll and follow bluecove installation instructions on Native Library location.
For example:
Create a directory c:\blue, and a directory blue\lib.
Put the files like this:
C:\blue\Sever.java
C:\blue\Sever.class
C:\blue\[bluecove_native_library].dll
C:\blue\lib\bluecove-2.1.0.jar
Open a cmd (Command Prompt) on c:\blue
Try to run the app like this:
java -cp .;lib\bluecove-2.1.0.jar -Dbluecove.native.resource=false Server
This way you will have both Server.class and bluecove-2.1.0.jar in classpath and the DLL in path (since on Windows the current dir is in path).
Add a current folder to a classpath (.;) and make sure that your code is in a package-name folder.
So if your server has a package name :
mypackage
make sure that your class code is in a /mypackage directory
than go to a parent folder to /mypackage and call:
java -cp ".;C:\Program Files\Java\bluecove-2.1.0.jar" mypackage.Server

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

Specifying Maven Directory when Running Java.exe

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

Having trouble running code in Eclipse using JOGL

I'm trying to run an application in eclipse which is setup as MVC app and it is the solution code posted by our professor for a homework. After I copy and pasted all of the classes and tried to run it, I got the following error:
JOGL> Hello JOAL Exception in thread "main" java.lang.NoClassDefFoundError: com/jogamp/openal/JoalVersion at jogamp.opengl.openal.av.ALDummyUsage.main(ALDummyUsage.java:14) Caused by: java.lang.ClassNotFoundException: com.jogamp.openal.JoalVersion at java.net.URLClassLoader$1.run(Unknown Source) 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) ... 1 more
I'm not entirely sure what the issue is and apparently no one else has been having the same issue.
My buildpath has a library I titled "JOGL" and I added the external jars gluegen-rt.jar and jogl-all.jar as was instructed by the professor.
Thanks for the help!
Actually, your code uses JOAL too, you need to put joal.jar into your classpath (Build path in Eclipse) and you need to put the JAR(s) containing its native libraries into the same directory as you did for JOGL and GlueGen. I assume you followed these instructions:
http://jogamp.org/wiki/index.php/Setting_up_a_JogAmp_project_in_your_favorite_IDE#Eclipse_IDE_project
Edit.: You don't run your own program for your homework, you try to run ALDummyUsage which is in JOGL itself. If you want to run your own program with your own main class, rather right-click on it and choose "Run As" -> "Java application" in Eclipse.

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