running junit testcase application from command line causes initialization errors - java

I have a testing application running as expected when executed on Eclipse IDE.
We want to export it to a jar and execute it via command line.
After executing, and running the application with the following comand, i encounter an error
java -jar testclient.jar -info
Error: Unable to initialize main class testclient.Main
Caused by: java.lang.NoClassDefFoundError: org/junit/runners/model/InitializationError
( -info) is an argument for the main method in the class.
Manifest file is pointing to the correct class.
On research of this topic, i checked whether the required libraries are added to the path. They are correctly added.
Could anyone provide some guidance

Related

How do I pass an additional jar do my java command when running application?

I have a java gradle projectA that references another java gradle projectB, that builds as a lib.
My gradle build configuration seems to be fine, since I can import and use classes from the other project, and it compiles. But when I try to run the application from the command line I get an error...
Exception in thread "main" java.lang.NoClassDefFoundError:
dawcore/SamplerInstrument at DawCLI.main(DawCLI.java:17) Caused by:
java.lang.ClassNotFoundException: dawcore.SamplerInstrument at
java.net.URLClassLoader.findClass(URLClassLoader.java:382) at
java.lang.ClassLoader.loadClass(ClassLoader.java:424) at
sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349) at
java.lang.ClassLoader.loadClass(ClassLoader.java:357) ... 1 more
It seems to be complaining that it can't load the classes in the jar at runtime....which makes sense. But I don't know how to have it successfully load those classes.
my current run command that does not reference the jar, is as follows....
/usr/lib/jvm/java-1.8.0-openjdk-amd64/jre/bin/java -ea -cp "build/classes/main/" DawCLI
Running this gives the initially mentioned error.
I then read the docs on java -cp argument. It says to provide additional classpath directories, to separate them by semicolon.
I susequently updated my run command to be as follows....
/usr/lib/jvm/java-1.8.0-openjdk-amd64/jre/bin/java -ea -cp "build/classes/main/;../DawCore/build/libs/DawCore.jar" DawCLI
This gives me the following error....
Error: Could not find or load main class DawCLI
My main function is as follows..
import dawcore.*;
public class DawCLI {
public static void main (String [] args) throws IOException {
SamplerInstrument samplerkick = new SamplerInstrument();
}
}
According to the docs I seem to be doing this correctly, but am still getting errors. Any insight on this would be greatly appreciated!
Thanks
The solution was to replace the delimiting semi-colon with a colon instead. Even though the first line of the javadocs on -cp command indicate that you should use a semi-colon.
This could be because I am running on linux and those docs indicate Windows.
https://docs.oracle.com/javase/7/docs/technotes/tools/windows/java.html

Unable to load type error in Randoop

When I try to run Randoop in the Windows terminal I get the following error:
C:\Users\dell13\workspace\project>java -classpath .;.\randoop-3.0.7\randoop-all-3.0.7.jar randoop.main.Main gentests --testclass=Factorial --timelimit=60
policy = sun.security.provider.PolicyFile#5fd0d5ae
Throwable thrown while handling command: java.lang.Error: Unable to load type "Factorial" due to exception: null
java.lang.Error: Unable to load type "Factorial" due to exception: null
at randoop.main.ThrowClassNameError.handle(ThrowClassNameError.java:16)
at randoop.reflection.OperationModel.addClassTypes(OperationModel.java:307)
at randoop.reflection.OperationModel.createModel(OperationModel.java:132)
at randoop.main.GenTests.handle(GenTests.java:193)
at randoop.main.Main.nonStaticMain(Main.java:63)
at randoop.main.Main.main(Main.java:27)
Randoop failed.
Last sequence under execution: null
The file I am trying to run Randoop on is Factorial.class which is located in the current working directory. The current working directory also contains the folder with all the randoop files/jars. I'm not sure why this is happening because I tried this on my Mac and it seemed to work just fine.
I am currently the primary developer of Randoop, so I can tell you that this error is always because Java cannot find the class in the classpath. This is either because the name does not match the name of a class in the classpath, or the classpath is not correct.
I don't have a Windows machine, but, all Oracle documentation aside, it would appear that you need to put the classpath in double quotes.

Exception Error in creating an instance of RandomDataImpl

I am getting this exception error at
Exception in thread "main" java.lang.NoClassDefFoundError:
org/apache/commons/math3/random/RandomDataImpl
at this line of code:
randomData_ = new RandomDataImpl();
and this is how the class is imported in the code :
import org.apache.commons.math3.random.RandomDataImpl;
i have added all of the math common classes to the classpath while running the code. any ideas whats going on ?
It looks like you did not set the appropriate classpath.
You can set it as follows:
java -classpath lib/*.jar:. mypackage.MyClass
Try that and I am sure it will work.
If you are using maven, then read this article
You can also check your current classpath with:
System.getProperty("java.class.path")
Just write simple app printing this property out and run it just as you run an app with which you are having problems.

UnsatisfiedLinkError with OpenCV in Tomcat

First of all, I have a basic example of OpenCV running in a main method.
However, if I use OpenCV in a Spring Web Controller, an error is thrown.
I am getting an UnsatisfiedLinkError when running the following code inside a Tomcat Server, within STS (Spring Tool Suite), and have boiled the problem down to the following code:
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
CascadeClassifier cascadeClassifier = new CascadeClassifier();
The run configurations of both the server and the "main" application contain the VM Arguments pointing to the OpenCV DLLs:
-Djava.library.path="C:\opencv\build\java\x64;C:\opencv\build\x64\vc10\bin"
For reference, I have also included the pertinent bits of the stacktrace below:
org.springframework.web.util.NestedServletException: Handler processing failed;
nested exception is
java.lang.UnsatisfiedLinkError: org.opencv.objdetect.CascadeClassifier.CascadeClassifier_0()J
...
Caused by: java.lang.UnsatisfiedLinkError:
org.opencv.objdetect.CascadeClassifier.CascadeClassifier_0()J
at org.opencv.objdetect.CascadeClassifier.CascadeClassifier_0(Native Method)
at org.opencv.objdetect.CascadeClassifier.<init>(CascadeClassifier.java:38)
at com.immersion.test.controllers.SimpleController.createClassifier(SimpleController.java:19)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
Note: I have read in several StackOverflow threads that the main cause of the UnsatisfiedLinkError is the missing System.loadLibrary(Core.NATIVE_LIBRARY_NAME) call. This is clearly not the case here, since it is being called just above where the exception is thrown.
Same thing here.
I try what Dylan said, but did not work here.
But I added "C:\opencv\build\java\x64;C:\opencv\build\x64\vc10\bin" on my PATH variable. On my sys variables.
ps: I'm using windows.
It turns out that the default run configuration to start a Tomcat server within STS/Eclipse is essentially just a launcher for Tomcat.
This means that any VM Args that are added to the Tomcat 'run configuration' will not be transferred to the actual Tomcat instance.
On looking in tcruntime-instance.bat within the Tomcat directory (this is what ends up creating the tomcat java instance), we can see that simply by adding our intended VM Args to the JAVA_OPTS environment variable, the dlls will be available to Tomcat.
TL;DR:
So.. Simply add the the locations of the OpenCV binaries to your JAVA_OPTS environment variable:
-Djava.library.path="C:\opencv\build\java\x64;C:\opencv\build\x64\vc10\bin"
For more information on setting JAVA_OPTS for Tomcat, see this SO question.

error in executing jar from commandline

When I fire the following from command line:
D:\workspace 2\project\lib>java -javaagent:myagent.jar -cp asm-all-3.3.jar;. AgentMain
I get the following error
java.lang.NullPointerException
java.lang.NullPointerException
Exception in thread "main" java.lang.NoSuchMethodError: java.lang.VerifyError: method(init) (Ljava/lang/String;) V not found
java.lang.NullPointerException
Couldnot find the main class AgentMain. Program will exit.
I have all the files and jar in the working directory. AgentMain is the compiled class that I put it here from the bin folder. I dont know what is the problem.
I would check first whether the Agent is implemented OK?
You did follow the structure as set forth in java.lang.instrument (public static void premain(String agentArgs);) including the MANIFEST entries? And there's no way that the Agent can throw NPEs during initialization?
Also, you may want to try out wheter it's the Agent or the AgentMain.class, by running java -javaagent:myagent.jar -cp asm-all-3.3.jar;. -version
Have you tried setting the classpath to the current folder (-cp . ) ?

Categories