I have the following code. After compilation I delete the MyClassToLoad.class file and run the code.
public class ClassLoadersTest {
public static void main(String[] args) {
MyClassToLoad c = new MyClassToLoad();
}
}
I get the following stacktrace:
Exception in thread "main" java.lang.NoClassDefFoundError:
classloaders/MyClassToLoad at
classloaders.ClassLoadersTest.main(ClassLoadersTest.java:9) at
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597) at
com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Caused by: java.lang.ClassNotFoundException:
classloaders.MyClassToLoad 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:303) at
sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301) at
java.lang.ClassLoader.loadClass(ClassLoader.java:248) at
java.lang.ClassLoader.loadClassInternal(ClassLoader.java:316)
... 6
more
As far as I understand, this stacktrace means that there was a ClassNotFoundException, that was caught and rethrown as a NoClassDefFoundError.
The questions are:
1) How can I understand at which line the rethrow happened?
2) Who cuts the stacktrace with "... 6 more" - Java or Idea? How can I see it full?
3) As far as I understand to force the rethrown exception contain the full stacktrace we need to rethrow it as
throw new SomeRethrownException("some text", exceptionWhichIsTheReason)
But the NoClassDefFoundError doesn't have such a constructor. So in fact it shouldn't print the full stacktrace.. or may be they just put it as Error message as a String?
classloaders.ClassLoadersTest.main(ClassLoadersTest.java:9) at <-- here
The runtime system. Try
public static void main(String[] args) {
try {
MyClassToLoad c = new MyClassToLoad();
} catch (java.lang.NoClassDefFoundErro e) {
e.getCause().printStackTrace();
}
}
As for 3, see my answer to 2.
Related
I'm using a Spring Boot APP and I'm trying to set a System property programatically. I don't want to set the URL hardcoded, it must be loaded from the classpath
The problem is that my code runs well on IDE but when I try to execute the jar file I get:
Exception in thread "main" java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48)
at org.springframework.boot.loader.Launcher.launch(Launcher.java:87)
at org.springframework.boot.loader.Launcher.launch(Launcher.java:51)
at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:52)
Caused by: java.nio.file.FileSystemNotFoundException
at com.sun.nio.zipfs.ZipFileSystemProvider.getFileSystem(ZipFileSystemProvider.java:171)
at com.sun.nio.zipfs.ZipFileSystemProvider.getPath(ZipFileSystemProvider.java:157)
at java.nio.file.Paths.get(Unknown Source)
My Code:
public static void main(String[] args) {
try {
setSystemProperties();
} catch (IOException e) {
log.error("ERROR: Could not find my.properties file in classpath", e);
}
}
private static void setSystemProperties() throws IOException {
ClassPathResource resource = new ClassPathResource("my.properties");
System.setProperty("my.file", Paths.get(resource.getURI()).toString());
}
InvocationTargetException occurs when there's some underlying exception while calling method. Add the below catch block in your code to see the actual cause.
catch (InvocationTargetException e) {
// the real cause
e.getCause().printStackTrace();
}
I'm relatively new (about 1 month) to JavaFX, and even newer to Gradle. My project is here, on Github. When I run ./gradlew build, it works fine. But, when I run ./gradlew run , I get all this:
Alins-MacBook-Pro:evisu Alin$ ./gradlew run
:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:cssToBin SKIPPED
:classes UP-TO-DATE
:run
Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$152(LauncherImpl.java:182)
at com.sun.javafx.application.LauncherImpl$$Lambda$51/1323468230.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException: inputStream is null.
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2459)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2429)
at ro.badilos.evisu.EviSU.loadMainPane(EviSU.java:43)
at ro.badilos.evisu.EviSU.start(EviSU.java:27)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$159(LauncherImpl.java:863)
at com.sun.javafx.application.LauncherImpl$$Lambda$54/2140593657.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$172(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl$$Lambda$47/186276003.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null$170(PlatformImpl.java:295)
at com.sun.javafx.application.PlatformImpl$$Lambda$49/1714838540.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$171(PlatformImpl.java:294)
at com.sun.javafx.application.PlatformImpl$$Lambda$48/237061348.run(Unknown Source)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
Exception running application ro.badilos.evisu.EviSU
:run FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':run'.
> Process 'command '/Library/Java/JavaVirtualMachines/jdk1.8.0_51.jdk/Contents/Home/bin/java'' finished with non-zero exit value 1
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
BUILD FAILED
Total time: 3.74 secs
What am I doing wrong?
Is this the base of all the problems?
Caused by: java.lang.NullPointerException: inputStream is null.
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2459)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2429)
at ro.badilos.evisu.EviSU.loadMainPane(EviSU.java:43)
at ro.badilos.evisu.EviSU.start(EviSU.java:27)
What would be the correct path to the .fxml files?
Thank you!
Yes, the NullPointerException is the root cause of the whole problem. You red resources incorrectly. Please replace this line (in class ro.badilos.evisu.EviSU:
Pane mainPane = (Pane) loader.load(getClass().getResourceAsStream(VistaNavigator.MAIN));
with the following one:
Pane mainPane = (Pane) loader.load(getClass().getClassLoader().getResourceAsStream(VistaNavigator.MAIN));
And change the following lines in ro.badilos.evisu.VistaNavigator:
public static final String MAIN = "src/main/resources/Main.fxml";
public static final String VISTA_1 = "src/main/resources/NewSU.fxml";
public static final String VISTA_2 = "src/main/resources/NewSMURD.fxml";
to:
public static final String MAIN = "Main.fxml";
public static final String VISTA_1 = "NewSU.fxml";
public static final String VISTA_2 = "NewSMURD.fxml";
After introducing these changes all resources will be read correctly. Running gradle clean run still results in exception but this is another one.
To fix the new exception change (in ro.badilos.evisu.VistaNavigator):
public static void loadVista(String fxml) {
try {
mainController.setVista(
(Node) FXMLLoader.load(
VistaNavigator.class.getResource(
fxml
)
));
} catch (IOException e) {
e.printStackTrace();
}
}
to:
public static void loadVista(String fxml) {
try {
mainController.setVista(
(Node) FXMLLoader.load(
VistaNavigator.class.getClassLoader().getResource(
fxml
)
));
} catch (IOException e) {
e.printStackTrace();
}
}
Remember to always refer to resources via ClassLoader. Now it runs, however another NPE is printed to console.
EDIT
Ok, the final NPE is cause by:
#Override
public void initialize(URL location, ResourceBundle resources) {
try {
Image focIcon = new Image(getClass().getResourceAsStream("pool/foc16px.png"));
btnNewSU.setGraphic(new ImageView(focIcon));
Image smurdIcon = new Image(getClass().getResourceAsStream("pool/smurd16px.png"));
btnNewSMURD.setGraphic(new ImageView(smurdIcon));
} catch (Exception e) {
e.printStackTrace();
}
}
Should be:
#Override
public void initialize(URL location, ResourceBundle resources) {
try {
Image focIcon = new Image(getClass().getClassLoader().getResourceAsStream("foc16px.png"));
btnNewSU.setGraphic(new ImageView(focIcon));
Image smurdIcon = new Image(getClass().getClassLoader().getResourceAsStream("smurd16px.png"));
btnNewSMURD.setGraphic(new ImageView(smurdIcon));
} catch (Exception e) {
e.printStackTrace();
}
}
And the last thing, you need to fix the reference to Nomeclator.db in ro.badilos.controller.InfoSituatiaProdusaController as well.
At the beginning I had an error java.lang.ClassNotFoundException. I solved this problem by adding jars to \war\WEB-INF\lib folder. But then I got java.lang.NoClassDefFoundError: Could not initialize class com.orientechnologies.orient.core.Orient error while applying to database. This is the server code:
public class GreetingServiceImpl extends RemoteServiceServlet implements
GreetingService {
public String greetServer(String input) throws IllegalArgumentException {
RegBugs rb = new RegBugs();
String a = rb.getDbAdres();
try {
//The error is here
ArrayList<String> table = rb.getAllGitRecords();
} catch (ParseException e) {
e.printStackTrace();
}
return a;
}
Compilation is finished successfully. And when I start to call to server I get An error occurred while attempting to contact the server. Please check your network connection and try again. from gwt. How could I solve this problem?
P.S. I use eclipse and gwt-plugin for it
The error occurs when I try to interrupt with orient database.
Trace:
Caused by: java.lang.NoClassDefFoundError: Could not initialize class com.orientechnologies.orient.core.Orient
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at com.google.appengine.tools.development.agent.runtime.RuntimeHelper.checkRestricted(RuntimeHelper.java:70)
at com.google.appengine.tools.development.agent.runtime.Runtime.checkRestricted(Runtime.java:64)
at com.orientechnologies.orient.core.db.raw.ODatabaseRaw.open(ODatabaseRaw.java:108)
at com.orientechnologies.orient.core.db.ODatabaseWrapperAbstract.open(ODatabaseWrapperAbstract.java:53)
at com.orientechnologies.orient.core.db.record.ODatabaseRecordAbstract.open(ODatabaseRecordAbstract.java:127)
at com.orientechnologies.orient.core.db.ODatabaseWrapperAbstract.open(ODatabaseWrapperAbstract.java:53)
at ru.sersem.testgwt.server.RegBugs.getBugRecordsCount(RegBugs.java:192)
at ru.sersem.testgwt.server.GreetingServiceImpl.greetServer(GreetingServiceImpl.java:22)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.google.appengine.tools.development.agent.runtime.Runtime.invoke(Runtime.java:115)
at com.google.gwt.user.server.rpc.RPC.invokeAndEncodeResponse(RPC.java:561)
... 40 more
In the following code, I have used a wrong file name (wrongFileName.fxml) to get loaded, in order to test catch block. However, the statements inside the catch block doesn't execute. Please note, even when there is no statement to print the stack trace on the console, still stack trace gets printed. Please let me know, why this is happening?
public class First extends Application {
#Override
public void start(Stage stage){
Parent root = null;
try {
root = FXMLLoader.load(getClass().getResource("wrongFileName.fxml"));
} catch (IOException ex) {
System.out.println("SomeTextForCheck");
}
}
}
Stack trace:
Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at com.javafx.main.Main.launchApp(Main.java:698)
at com.javafx.main.Main.main(Main.java:871)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:403)
at com.sun.javafx.application.LauncherImpl.access$000(LauncherImpl.java:47)
at com.sun.javafx.application.LauncherImpl$1.run(LauncherImpl.java:115)
at java.lang.Thread.run(Thread.java:744)
Caused by: java.lang.NullPointerException: Location is required.
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2773)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2757)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2743)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2730)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2719)
at first.First.start(First.java:29)
at com.sun.javafx.application.LauncherImpl$5.run(LauncherImpl.java:319)
at com.sun.javafx.application.PlatformImpl$5.run(PlatformImpl.java:216)
at com.sun.javafx.application.PlatformImpl$4$1.run(PlatformImpl.java:179)
at com.sun.javafx.application.PlatformImpl$4$1.run(PlatformImpl.java:176)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl$4.run(PlatformImpl.java:176)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:76)
at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
at com.sun.glass.ui.gtk.GtkApplication$3$1.run(GtkApplication.java:89)
... 1 more
From the stack trace, RuntimeException is thrown. So Kindly handle the RuntimeException and Exception class in your code. Always log the exception or use stacktrace otherwise its difficult to debug.
public class First extends Application {
#Override
public void start(Stage stage){
Parent root = null;
try {
root = FXMLLoader.load(getClass().getResource("wrongFileName.fxml"));
} catch (IOException ex) {
System.out.println("SomeTextForCheck");
ex.printStackTrace()
}
catch(RuntimeException ex) {
System.out.println("SomeTextForCheck");
ex.printStackTrace()
}
catch(Exception ex) {
System.out.println("SomeTextForCheck");
ex.printStackTrace()
}
}
You need to catch the right exception to print the stack trace. Ideally you shouldn't be getting exceptions like NullPointerException, developers need to handle such conditions in the code.
I've just switched my java program to use openjdk6 from java 1.7 and now it wont compile. Can anyone help, im a long time developer but newish in Java.
Abnormal build process termination:
Exception in thread "main" java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:622)
at org.jetbrains.jps.cmdline.Launcher.main(Launcher.java:51)
Caused by: java.lang.NoSuchFieldError: family
at sun.nio.ch.ServerSocketChannelImpl.initIDs(Native Method)
at sun.nio.ch.ServerSocketChannelImpl.<clinit>(ServerSocketChannelImpl.java:346)
at sun.nio.ch.SelectorProviderImpl.openServerSocketChannel(SelectorProviderImpl.java:51)
at java.nio.channels.ServerSocketChannel.open(ServerSocketChannel.java:92)
at sun.nio.ch.PipeImpl$Initializer.run(PipeImpl.java:89)
at java.security.AccessController.doPrivileged(Native Method)
at sun.nio.ch.PipeImpl.<init>(PipeImpl.java:140)
at sun.nio.ch.SelectorProviderImpl.openPipe(SelectorProviderImpl.java:45)
"main" looks like
import app.Controller;
import quickfix.ConfigError;
import static libs.Library.log;
public class Main
{
public static void main(String[] args)
{
try {
log("Starting...");
Class.forName("com.mysql.jdbc.Driver").newInstance();
Controller client = new Controller();
client.login();
synchronized (client)
{
try {
client.wait();
}
catch (Exception e) {}
}
}
catch (ConfigError e) {
log("Config error: " + e.getMessage());
}
catch (Exception e) {
log(e.getMessage());
}
}
}
Stacktrace is showing that you are trying to run it from inside Intellij IDEA. Injellij IDEA doesn't guarantee that it will work on OpenJDK. A quote from https://intellij-support.jetbrains.com/entries/23455956-Selecting-the-JDK-version-the-IDE-will-run-under
Using OpenJDK is not recommended, it has known visual and performance problems that affect some users. Use on your own risk if Oracle JDK has more severe issues on your system.
Try compiling and running from command line. But if you want to keep using Intellij IDEA switch to Oracle JDK.