I'm getting NoClassDefFoundError exception when I run this main class through Eclipse
import org.json.simple.parser.JSONParser;
public class A {
public static void main(String args[]) throws IOException {
JSONParser jsonParser = new JSONParser();
// code
}
}
Exception:
Exception in thread "main" java.lang.NoClassDefFoundError: org/json/simple/parser/JSONParser
Caused by: java.lang.ClassNotFoundException: org.json.simple.parser.JSONParser
I am implementing below dependency in gradle file:
implementation 'com.googlecode.json-simple:json-simple:1.1.1'
Have you changed/set your classpath to your current project? Maybe that's why your program is giving you error
Related
Please find the below code i am trying to execute , I am getting java.lang.NullPointerException error when i try to run it as TestNG Test
import org.testng.annotations.Test;
public class First
{
#Test
public void dispaly()
{
System.out.println("Hello World");
}
}
Error :
An internal error occurred during: "Launching First".
java.lang.NullPointerException
It's very interesting to use Class.forName() to load a class and get errors... I write one line code and get an error as above.
class UnsatisfiedLinkErrorTest {
public static void main(String[] args) throws ClassNotFoundException {
System.out.println(Class.forName("java.net.SocketOutputStream"));
}
}
When I use java UnsatisfiedLinkErrorTest, I get an error:
> java UnsatisfiedLinkErrorTest
Exception in thread "main" java.lang.UnsatisfiedLinkError: java.net.SocketOutputStream.init()V
at java.net.SocketOutputStream.init(Native Method)
at java.net.SocketOutputStream.<clinit>(SocketOutputStream.java:44)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:259)
at UnsatisfiedLinkErrorTest.main(UnsatisfiedLinkErrorTest.java:3)
But when I run it in Idea, I get:
> java "-javaagent:/Applications/IntelliJ IDEA.app/Contents/lib/idea_rt.jar=49247:/Applications/IntelliJ IDEA.app/Contents/bin" UnsatisfiedLinkErrorTest
class java.net.SocketOutputStream
I don't know why in Idea I can get the true answer, (maybe jni?) I hope someone can tell me the reason. Thanks.
This question has been solved. I then cancelled the initialization of the class, then the error goes. The error is about the initialization, but not about finding the native library. So:
class UnsatisfiedLinkErrorTest {
public static void main(String[] args) throws ClassNotFoundException {
System.out.println(Class.forName("java.net.SocketOutputStream", false, null)); // will be okay!
}
}
I have the typical error in Java. I have the next structure:
bin/
lib/
src/
junior/
databases/
homework/Main.java
My Main.java code is:
package junior.databases.homework;
import java.sql.*;
public class Main {
private static Connection connection = null;
public static void main(String[] args) throws SQLException, ClassNotFoundException {
initDatabase();
System.out.println("Done");
}
private static void initDatabase() throws SQLException, ClassNotFoundException {
Class.forName("org.postgresql.Driver");
connection = DriverManager.getConnection(
"jdbc:postgresql://192.168.136.129:5432/postgres", "postgres", "xxxx");
}
}
When I launch it like this:
root#debian:/python_codes/Junior/Level1/DB1/ORM/java/java/src/junior/databases/homework# javac Main.java
root#debian:/python_codes/Junior/Level1/DB1/ORM/java/java/src/junior/databases/homework#java Main
I got error:
Exception in thread "main" java.lang.NoClassDefFoundError: Main (wrong name: junior/databases/homework/Main)
I found in this post the solution Exception in thread "main" java.lang.NoClassDefFoundError: Hello
And my try:
root#debian:/python_codes/Junior/Level1/DB1/ORM/java/java/src# javac junior/databases/homework/Main.java
root#debian:/python_codes/Junior/Level1/DB1/ORM/java/java/src# java junior.databases.homework.Main
works perfectly.
The problem is I can launch this code only from src/ folder((( Is there any way I can launch it from /src/junior/databases/homework folder? I need to go back to src each time I want to launch the code.
Here you are, assuming you are in src/junior/databases/homework directory:
javac ../../../junior/databases/homework/Main.java
java -cp ../../../ junior.databases.homework.Main
I developed a JAVA (JDK1.6) application to manage PDF file with iText (v5.5.0).
After I wrote test application using groovy, but when i create a PdfReader object, in my test case,
PdfReader pdfReader = new PdfReader("/my/path/project/test.pdf")
I obtain the following error:
java.lang.NoClassDefFoundError: org/bouncycastle/cms/RecipientId
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2484)
...
Caused by: java.lang.ClassNotFoundException: org.bouncycastle.cms.RecipientId
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
The first asserts in groovy test class works fine.
I created the same test class with JUnit4, and all works fine!
How can I fix the error in groovy test class?
I don't use bouncycastle class, why have I that ClassNotFound Exception?
EDIT:
GroovyTestCase
class PdfMergeItextTest extends GroovyTestCase {
PdfMergeItext pdfMerge
void setUp() {
super.setUp();
println "Test class [PdfMergeItext] avviato..."
pdfMerge = new PdfMergeItext()
pdfMerge.openOutputPdf("/my/path/project/output.pdf")
}
void tearDown() {
println "Test class [PdfMergeItext] END."
}
#Test
void testMergeSinglePdfFile() {
println "Test one Pdf.."
File outputPdf = new File("/my/path/project/output.pdf")
assertTrue outputPdf.exists()
PdfReader pdfReader = new PdfReader("/my/path/project/test.pdf")
pdfMerge.addPdf(pdfReader)
pdfMerge.flush()
pdfMerge.close()
assert outputPdf.size() > 0
println "File size: ${outputPdf.size()}"
println "End test one Pdf"
}
}
JUnit4 TestCase
public class PdfMergeItextUnitTest {
PdfMergeItext pdfMergeItext = null;
#Before
public void setUp() throws Exception {
System.out.println("Start..");
this.pdfMergeItext = new PdfMergeItext();
this.pdfMergeItext.openOutputPdf("/my/path/project/output.pdf");
}
#After
public void tearDown() throws Exception {
System.out.println("END!");
}
#Test
public void testMergePdfFile() throws IOException, BadPdfFormatException {
File outputPdf = new File("/my/path/project/output.pdf");
Assert.assertTrue(outputPdf.exists());
PdfReader pdfReader = new PdfReader("/my/path/project/test.pdf");
this.pdfMergeItext.addPdf(pdfReader);
this.pdfMergeItext.flush();
this.pdfMergeItext.close();
Assert.assertTrue(outputPdf.size() > 0);
}
}
Thanks
The BounceCastle JARs are optional, see the pom.xml (search for dependency). So, if you didn't include them in your pom.xml or build.gradle , you won't get them (anyway, what build tool are you using)?
So if you are using dependency management I understand why it is not there. The real question is: why is it needed in the first test and not in the second test?
Please make the tests totally equalent (you are calling addPdf once vs. twice and you are calling length vs. size
Since BounceCastle is used for decryption, are you testing with the same PDF file?
My Main
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp {
public static void main(String[] args) {
System.out.println("hola");
ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
HelloWorld obj = (HelloWorld) context.getBean("helloWorld");
obj.getMessage();
}
}
Exception in thread "main" java.lang.ExceptionInInitializerError
at org.springframework.context.support.AbstractRefreshableApplicationContext.createBeanFactory(AbstractRefreshableApplicationContext.java:201)
at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:127)
at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:551)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:465)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at com.tutorialspoint.MainApp.main(MainApp.java:9)
Caused by: java.lang.NullPointerException
at org.springframework.beans.factory.support.
DefaultListableBeanFactory.<clinit>(DefaultListableBeanFactory.java:108)
... 7 more
The NullPointerException error is at almost impossible location:
static {
ClassLoader cl = DefaultListableBeanFactory.class.getClassLoader();
try {
javaxInjectProviderClass = cl.loadClass("javax.inject.Provider"); /* line 108 */
} catch (ClassNotFoundException ex) {
// JSR-330 API not available - Provider interface simply not supported then.
}
}
This means that the class is not able to get its own classloader. You must have done something really bad to get this error. Check your JRE/JDK, IDE, ...
UPDATE
There is no explanation other than that you are probably trying to put Spring JARs into JRE's library folder (${java.home}/jre/lib). If that is the case, that is simply wrong. If you really want to include external JARs within JRE, then put them in the official extension directory - ${java.home}/jre/lib/ext.