I am trying to create a runnable jar file that has some TestNG tests.I can run the project files from Eclipse but when I create a jar file it can't find the required library. A possible classpath issue I guess.There are similar questions on the forum but none of them have any answers that could help me resolve this issue. When I run the jar file id gives the following error.
Exception in thread "main" java.lang.NoClassDefFoundError: org/testng/TestNG
at test.TestRunner.main(TestRunner.java:11)
Caused by: java.lang.ClassNotFoundException: org.testng.TestNG
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:606)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:168)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
... 1 more
This is my Class with the main method.
package test;
import org.testng.TestNG;
public class TestRunner {
static TestNG testNG;
public static void main(String[] args) {
testNG = new TestNG();
testNG.setTestClasses( new Class[] {App1.class});
testNG.run();
}
}
Then there is a class that has some very simple tests.Everything runs fine from eclipse but the jar file has the problem. The problem seems very simple but yet I have not been able to resolve it. I would appreciate any help. Thanks
Related
I am trying to use the Jython PythonInterpreter class, but am struggling a bit.
Due to physical limitations, my scenario lacks any sort of project structure. I simply have a Jython JAR file and a Java file.
I am currently compiling my Java code like so:
javac -cp "jython-standalone-2.7.0.jar" test.java
This command does not fail.
When I try to run the compiled Java, I get this error:
Exception in thread "main" java.lang.NoClassDefFoundError: org/python/util/PythonInterpreter
at test.main(test.java:9)
Caused by: java.lang.ClassNotFoundException: org.python.util.PythonInterpreter
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 1 more
The code looks like so:
import org.python.util.PythonInterpreter;
class test {
public static void main(String[] args) {
PythonInterpreter interpreter = new PythonInterpreter();
}
}
The code is very simple. Should I be importing the Jython classes a different way? Does my method here have any chance of working?
Thanks
How do you run the command?
You will also need to set the CLASSPATH.
This is my java code
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class PlutoMake {
public static void main(String[] args) throws JSONException {
}
}
and I compile it with this and it compiles without error
javac PlutoMake.java -cp java-json.jar
Now I copied this project from eclipse from windows, and using that jar it worked fine. I could access all the json functions. But here on linux, when I run it, I get
Exception in thread "main" java.lang.NoClassDefFoundError: org/json/JSONException
Caused by: java.lang.ClassNotFoundException: org.json.JSONException
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:323)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:268)
If I remove the throws JSONException then I can run it. So for some reason I can import the jar but I can't access any of its functions/exceptions without crashing my program...
Jar file is from the answer from this link Importing JSON into an Eclipse project
Does anyone know how to fix this?
Thanks
Add the jar file to the runtime classpath
java -cp java-json.jar:. PlutoMake
When you have create project in eclipse on linux you need configure buildpath inserting the jar
I am interested in sending tweets using Java program.I wrote the following prgram.It doen't show any error in compiling.But while executing it shows
"Exception in thread "main" java.lang.NoClassDefFoundError: hi
Caused by: java.lang.ClassNotFoundException: hi
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
Could not find the main class: hi. Program will exit."
This is my code..
package twitter;
//import java.lang.object;
import net.unto.twitter.*;
import net.unto.twitter.TwitterProtos.Status;
public class hi
{
public static void main(String args[])
{
Api api = Api.builder().username("usename").password("password").build();
api.updateStatus("This is a test message.").build().post();
}
}
Can anybody help me ..Pls
Class named hi is within the package twitter.
Include the package name while executing the class.
Try this :
java -cp .:hello_twitter/java-twitter.jar twitter.hi
This error shows that you didn't start the application correctly. Could be a typical classpath problem, because you need a java library plus your own clode. Try this on a console/terminal:
(1) navigate to the root folder of you application (./twitter/hi.class should be a valid file from your actual path)
(2) execute the application with:
java -cp .;path/to/twitter/lib/twitterlib.jar twitter.hi
(replace path/to/twitter/lib/twitterlib.jar with the real path and lib name)
So, back to the basics:
You have class named twitter.hi, which needs to be stored in the file ./twitter/hi.java.
Then you must compile the java file to a file ./twitter/hi.class
When this is done, run the application with
java -cp .:hello_twitter/java-twitter.jar twitter.hi
The code below generates an error using Guice 2.0. With Guice 1.0 everything is fine. The JDK is Java 6 update 15.
public class App {
public static void main(String[] args) {
Guice.createInjector(new AbstractModule() {
#Override
protected void configure() {
// just testing
}
});
}
}
The error is:
Exception in thread "main" java.lang.NoClassDefFoundError: [Lorg/aopalliance/intercept/MethodInterceptor;
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2427)
at java.lang.Class.getDeclaredMethods(Class.java:1791)
at com.google.inject.internal.ProviderMethodsModule.getProviderMethods(ProviderMethodsModule.java:78)
at com.google.inject.internal.ProviderMethodsModule.configure(ProviderMethodsModule.java:70)
at com.google.inject.spi.Elements$RecordingBinder.install(Elements.java:223)
at com.google.inject.spi.Elements$RecordingBinder.install(Elements.java:232)
at com.google.inject.spi.Elements.getElements(Elements.java:101)
at com.google.inject.InjectorShell$Builder.build(InjectorShell.java:135)
at com.google.inject.InjectorBuilder.build(InjectorBuilder.java:102)
at com.google.inject.Guice.createInjector(Guice.java:92)
at com.google.inject.Guice.createInjector(Guice.java:69)
at com.google.inject.Guice.createInjector(Guice.java:59)
at App.main(App.java:6)
Caused by: java.lang.ClassNotFoundException: org.aopalliance.intercept.MethodInterceptor
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
... 14 more
What can be the problem?
You have missed to include a dependency jar with the class org.aopalliance.intercept.MethodInterceptor in the classpath.
as Boris Pavlović mentions in his answer, you're missing a jar. Specifically the aopalliance.jar file, which comes in the guice zip file
Alternatively, you might try using guice-2.0-no_aop.jar, but I'm not sure if that will work.
Adding this file to the classpath depends on which tool you use to run your java code.
If you are running java from the command line:
windows: java -cp aopalliance.jar;guice-2.0.jar;other_jars.jar YourMainClass
*nix: java -cp aopalliance.jar:guice-2.0.jar:other_jars.jar YourMainClass
If you are running java from Eclipse,
typically you'll have some type of
lib/ directory. Put your jar there,
then right-click on the jar -> Build
Path -> Add to Build Path
I run into this problem yesterday, the dependency is managed by Maven, so the aopalliance.jar is in classpath with no doubt.
Update jetty from version 6.1.10 to 6.1.26 fixed the problem.
The real problem here is the container does not support aopalliance.jar. I hope that will help someone there desperately looking for a solution.
I run into this problem today. Actually the tomcat can not find the class in its lib. So just copy the aopalliance.jar into the lib folder in tomcat ,that's enough.
Adding the below jars works fine for me.
javax.inject.jar
guice-3.0-no_aop.jar
I cloned my project from GitHub and now I am not able to run my project.
package edu.test.algorithm;
import edu.test.algorithm.Global;
public class Main {
public static void main(String[] args) {
Global global= new Global();
global.runGlobal("-----url");
}
}
I am getting this error:
Exception in thread "main" java.lang.ClassNotFoundException: edu.test.algorithm.Global.Main
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:122)
Any idea what's causing this error?
EDIT
I checked this question Intellij suddenly throwing ClassNotFoundExceptionbefore i posted here and it doesn't give me any solution.. Why marking my question as duplicate since that one isn't solving my problem?
Maybe your Ide is wrong run/debug configuration
Main -> Edit Configuration
enter image description here
Check your IDE run configurations and Class
https://www.jetbrains.com/help/idea/2016.2/creating-and-editing-run-debug-configurations.html
The exception message hints that you have used the wrong class name when launching the program.
You are (presumably) using edu.test.algorithm.Global.Main as the class name. However you have actually declared Main in the package edu.test.algorithm which means that its full classname is actually edu.test.algorithm.Main.
If you didn't do this explicitly, it is possible1 that you changed the Main classes package and neglected to change (or remake) the launcher config.
1 - I'm not familiar with Intellij ... but I know you can run into this kind of problem with Eclipse.