API works with Java application but not JavaFX - java

I am building a software in Java and JavaFX. It generates PDF reports (with DynamicReports) then uses SendGrid API to send to send them. I have already added the SendGrid API .jar file to the classpath.
The problem is that my JavaFX application crashes when I instanciate my subclass of SengGrid. As you can see:
Exception in thread "JavaFX Application Thread" java.lang.NoClassDefFoundError: com/sendgrid/SendGrid$Email
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:760) at java.lang.ClassLoader.defineClass(ClassLoader.java:760)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
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 Reporting.TableReport.lambda$build$0(TableReport.java:160)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.ClassNotFoundException: com.sendgrid.SendGrid$Email
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)
... 20 more
Now the interesting part is that the API works perfectly when I call it from a non JavaFX project. And more interestingly, it also works when I call it from a "console" main class inside the very same JavaFX project.
So far, I have tried to call that "console" main from my JavaFX code as a sort of static method that would fulfill my task (sending the PDF files). But again, I encounter the very same exception.
Can anyone suggest a solution to solve this ? (or at least go around it)

Turns out it was an issue with Netbeans. Using the exact same code with Eclipse works perfectly. I couldn't find an explanation but I'm giving this answer for anyone who might stumble upon the same issue as me.

Related

JavaFx not loading FXML

I create simple FXML in java and make little bit change of it like adding framework maven into project and changing FXML file into resource folder but when I try to run it it gives following errors.
Exception in thread "main" java.lang.ClassNotFoundException: sample.Main
at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:260)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:123)
But I change package's name from sample to other name. When I make name of package into sample it works fine but When I change it other name it dosn't work. what might be problem here??

Java FX Packager Error IntelliJ

I am trying to build a project of mine as an application. I have set up a 'JavaFX Application' artifact which has been set up correctly as it works on my friends computer.
When I run the program and it attempts to complete the build, I get the following error:
Error:Java FX Packager: java.lang.NoClassDefFoundError: org/apache/tools/ant/launch/AntMain
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at java.lang.ClassLoader.loadClass(ClassLoader.java:411)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:411)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at org.apache.tools.ant.launch.Launcher.run(Launcher.java:278)
at org.apache.tools.ant.launch.Launcher.main(Launcher.java:109)
Caused by: java.lang.ClassNotFoundException: org.apache.tools.ant.launch.AntMain
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 16 more
ant.home: /Applications/IntelliJ IDEA CE.app/Contents/lib/ant
Classpath: /Applications/IntelliJ IDEA CE.app/Contents/lib/ant/lib/ant.jar:/Applications/IntelliJ IDEA CE.app/Contents/lib/ant/lib/ant-launcher.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_101.jdk/Contents/Home/lib/ant-javafx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_101.jdk/Contents/Home/jre/lib/jfxrt.jar:/Applications/IntelliJ IDEA CE.app/Contents/lib/ant/lib/ant-antlr.jar:/Applications/IntelliJ IDEA CE.app/Contents/lib/ant/lib/ant-apache-bcel.jar:/Applications/IntelliJ IDEA CE.app/Contents/lib/ant/lib/ant-apache-bsf.jar:/Applications/IntelliJ IDEA CE.app/Contents/lib/ant/lib/ant-apache-log4j.jar:/Applications/IntelliJ IDEA CE.app/Contents/lib/ant/lib/ant-apache-oro.jar:/Applications/IntelliJ IDEA CE.app/Contents/lib/ant/lib/ant-apache-regexp.jar:/Applications/IntelliJ IDEA CE.app/Contents/lib/ant/lib/ant-apache-resolver.jar:/Applications/IntelliJ IDEA CE.app/Contents/lib/ant/lib/ant-apache-xalan2.jar:/Applications/IntelliJ IDEA CE.app/Contents/lib/ant/lib/ant-commons-logging.jar:/Applications/IntelliJ IDEA CE.app/Contents/lib/ant/lib/ant-commons-net.jar:/Applications/IntelliJ IDEA CE.app/Contents/lib/ant/lib/ant-jai.jar:/Applications/IntelliJ IDEA CE.app/Contents/lib/ant/lib/ant-javamail.jar:/Applications/IntelliJ IDEA CE.app/Contents/lib/ant/lib/ant-jdepend.jar:/Applications/IntelliJ IDEA CE.app/Contents/lib/ant/lib/ant-jmf.jar:/Applications/IntelliJ IDEA CE.app/Contents/lib/ant/lib/ant-jsch.jar:/Applications/IntelliJ IDEA CE.app/Contents/lib/ant/lib/ant-junit.jar:/Applications/IntelliJ IDEA CE.app/Contents/lib/ant/lib/ant-junit4.jar:/Applications/IntelliJ IDEA CE.app/Contents/lib/ant/lib/ant-launcher.jar:/Applications/IntelliJ IDEA CE.app/Contents/lib/ant/lib/ant-netrexx.jar:/Applications/IntelliJ IDEA CE.app/Contents/lib/ant/lib/ant-swing.jar:/Applications/IntelliJ IDEA CE.app/Contents/lib/ant/lib/ant-testutil.jar:/Applications/IntelliJ IDEA CE.app/Contents/lib/ant/lib/ant.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_101.jdk/Contents/Home/lib/tools.jar
Launcher JAR: /Applications/IntelliJ IDEA CE.app/Contents/lib/ant/lib/ant-launcher.jar
Launcher Directory: /Applications/IntelliJ IDEA CE.app/Contents/lib/ant/lib
Im really unsure how to go about fixing this. Anyone know what it could be?

NoClassDefFoundError Htmlunit

I'm new to HtmlUnit. I tried a simple code for loading a web page using htmlunit and use the xpath to get to a specific div tag.
The xpath is correct. I have verified using the console.
The code that so far I tried is
import java.util.List;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
public class Test {
public static void main(String s[]) throws Exception
{
WebClient client = new WebClient();
HtmlPage page = client.getPage("http://jpu1.pv.sv.nextag.com/serv/iinedan/buyer/outPdir.jsp?search=camera");
List<?> items = page.getByXPath("//div[contains(#class,'search-result-cards')]//div[contains(#class,'listContainer')]");
for(HtmlElement item : (List<HtmlElement>)items)
{
List<?> imageContainer;
imageContainer = item.getByXPath("//div[contains(#class,'imageContainer')]/a");
System.out.println("Image container data length : "+imageContainer.size());
System.out.println("Image "+imageContainer.toString());
}
}
}
But when I execute this code, I'm facing the following exceptions.
Exception in thread "main" java.lang.NoClassDefFoundError: org/w3c/dom/ElementTraversal
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at Test.main(Test.java:11)
Caused by: java.lang.ClassNotFoundException:
org.w3c.dom.ElementTraversal
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 37 more
It seems to like its a configuration isssue. I downloaded the jar from the site and added it to my refrenced library. IDE that I'm using is eclipse. HtmlUnit version is htmlunit-2.13.jar
The org/w3c/dom/ElementTraversal present in library xml-apis-1.4.01.jar on which htmlunit depends on. So, as a solution you need to put this jar in your project's classpath.
However, htmlunit depends on number of libraries. And it could happen that, after you resolved the current error, you will get a noClassDefFoundError for some other class.
In order to resolve it easily, start using Maven.
Create a maven project.
Add an entry for htmlunit dependency in pom.xml as follows :
<dependencies>
<dependency>
<groupId>net.sourceforge.htmlunit</groupId>
<artifactId>htmlunit</artifactId>
<version>2.13</version>
</dependency>
</dependencies>
Now create a new class and add your code there.
Maven will ensure, all transitive dependencies of any library to be available and keep those in project classpath automatically (if in IDE).
Many jar package includes org.w3c.dom.ElementTraversal.
Such as xml-apis, batik-ext, jython.
I think jar package conflict maybe cause your problem.
Can you give more jar package version detail?

netbeans javafx hello world sample [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I'm kinda new to Java, and I'm trying out JavaFX. I'm using NetBeans 8.2 and made a new JavaFX project. Netbeans created a sample "Hello world" app, but running it gave me this error:
Executing C:\Users\*****\Documents\NetBeansProjects\HelloWorld\dist\run875675325\HelloWorld.jar using platform C:\Program Files\Java\jdk1.8.0_20\jre/bin/java
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:483)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:363)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:303)
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:483)
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:875)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$147(LauncherImpl.java:157)
at com.sun.javafx.application.LauncherImpl$$Lambda$48/752848266.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.ClassFormatError: Truncated class file
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:760)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:455)
at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:367)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$153(LauncherImpl.java:819)
at com.sun.javafx.application.LauncherImpl$$Lambda$51/1073159910.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$166(PlatformImpl.java:323)
at com.sun.javafx.application.PlatformImpl$$Lambda$45/584634336.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null$164(PlatformImpl.java:292)
at com.sun.javafx.application.PlatformImpl$$Lambda$47/670904707.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$165(PlatformImpl.java:291)
at com.sun.javafx.application.PlatformImpl$$Lambda$46/501263526.run(Unknown Source)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$141(WinApplication.java:102)
at com.sun.glass.ui.win.WinApplication$$Lambda$37/96639997.run(Unknown Source)
... 1 more
Exception running application helloworld.HelloWorld
Java Result: 1
Any help would be appreciated.
The key thing in that stack trace is this line:
Caused by: java.lang.ClassFormatError: Truncated class file
Anything that says Error within a stacktrace points to really severe "environmental" kind of problems.
In your case: it seems that one (or more) of the class files that your IDE generates out of java source code is simply corrupted.
Try to to simple clean and rebuild your project.
If that doesn't help (which I would find rather surprising); then I am kinda worried that your Netbeans installation itself is somehow corrupted (so, me personally, I would then completely wipe that installation and try from scratch again).

Java JSON Jar NoClassDefFound Caused by ClassNotFoundException

I have a simple java file packaged as a Jar on a server called by a PHP script. I have the "json-simple-1.1.1" library included in my java program. However, when I run it gives me the below error. I've added the json-simple jar to my project and import the correct classes. From what I've read it doesn't seem to be adding the json-simple library to my manifest file. Any ideas on how to fix this. I'm using eclipse if that helps.
Exception in thread "main" java.lang.NoClassDefFoundError: org/json/simple/parser/ParseException
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2570)
at java.lang.Class.getMethod0(Class.java:2813)
at java.lang.Class.getMethod(Class.java:1663)
at sun.launcher.LauncherHelper.getMainMethod(LauncherHelper.java:494)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:486)
Caused by: java.lang.ClassNotFoundException: org.json.simple.parser.ParseException
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
Your library is probably not been loaded by classloader, so, if it is not in WEB-INF/lib, right click it and Add to build Path.

Categories