Resources not found after building a project in Netbeans 12 - java

I have a java application that runs correctly when I execute it from Netbeans.
If I execute it as a .jar it does not work. I am getting the error "Caused by: java.lang.NoClassDefFoundError: org/json/simple/parser/ParseException"
The error is ocurring in this command:
Object object = parser.parse(new FileReader("src\\main\\resources\\companies.json"));
I think the problem is that I am not defining the resource paths properly. See screenshot
---------------UPDATE-------------------------
After spending many hours trying to solve this s*** problem, I still can not run the application from a jar. I also try to call the ressources like that:
InputStream inputStream = NewMain.class.getResourceAsStream("/com/companies.json");
but the same problem as before. When I start the applicaion from a batch file I get this message:
I am totally blocked. I would really apreciate if someone could help me

How are you generating the jar? This seems like the json class cannot be found at runtime and you need to add it to the classpath when building.
You can also check the responses at java.lang.NoClassDefFoundError: org/json/simple/parser/ParseException with eclipse and spring.

Related

AWS Beanstalk + Kotlin + Spring boot: NoSuchMethodException main()

I've a Spring Boot app written in Kotlin that I have deployed to AWS Beanstalk that is throwing the following exception:
Exception in thread "main" java.lang.NoSuchMethodException: com.prototype.demo.DemoApplication.main([Ljava.lang.String;)
at java.base/java.lang.Class.getDeclaredMethod(Class.java:2675)
at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:47)
Locally, when I run the app from IntelliJ I've no issues.
I have the spring-boot-maven-plugin and the kotlin-maven-plugin in my project.
Any ideas about what could be causing the issue? I previously built a similar app in Java but didn't run into this issue.
Thank you for your expertise and time.
Update: something interesting to notice is that I had to set in the properties of my POM the following line in order to make the project executable by IntelliJ during development (i had forgotten about it): <start-class>com.prototype.demo.DemoApplication</start-class>
Now I've removed it and I get a different error when running the jar file. I will spend some time on it and see if that's related or not and update the question accordingly.
Thank you!
The issue was raised by the following property declared in my POM file: <start-class>com.prototype.demo.DemoApplication</start-class>.
Once I removed it and added the open keyword to the Configuration annotated classes and to the DemoApplication(the class containing the main) it finally worked.
Solution for build.gradle.kts
I had the same problem when deploying my application to ECS.
First things first:
When you see such an error in AWS logs -> do as the author of the question has done and test the issue locally. Most probably if the application is failing on AWS, it will also fail on you local machine. To do so, you build a jar locally, the jar file is usually saved in build/libs/application-x.x.x-SNAPSHOT.jar and run java -jar application-x.x.x-SNAPSHOT.jar
Now you can fix your build task and test it locally before deploying it again.
Fix Gradle:
My main method was defined in MyApplication.kt file, but in the definition of the main class in gradle I had to set the following:
tasks.getByName<org.springframework.boot.gradle.tasks.bundling.BootJar>("bootJar") {
mainClass.set("com.company.app.MyApplicationKt")
}
Notice: MyApplicationKt
That is the only thing I had to change for me to fix this issue.

Running OptaPlanner on the module path

I would like to make my OptaPlanner project run on the module path.
I created an example repository (mwkroening/optaplanner-modulepath-example) based on the cloud balancing demo in the docs.
The example project is running well on the classpath but there is an issue I can't workaround when trying to make it run on the module path. You can see the progress so far in PR #1.
The scoreDrl specified in the solverConfig.xml fails to load with the following exception:
Exception in thread "main" java.lang.IllegalArgumentException: The scoreDrl (io/github/mwkroening/optaplannermodulepathexample/cloudBalancingScoreRules.drl) does not exist as a classpath resource in the classLoader (jdk.internal.loader.ClassLoaders$AppClassLoader#7c16905e).
at org.optaplanner.core#7.18.0.Final/org.optaplanner.core.config.score.director.ScoreDirectorFactoryConfig.buildDroolsScoreDirectorFactory(ScoreDirectorFactoryConfig.java:519)
at org.optaplanner.core#7.18.0.Final/org.optaplanner.core.config.score.director.ScoreDirectorFactoryConfig.buildScoreDirectorFactory(ScoreDirectorFactoryConfig.java:351)
at org.optaplanner.core#7.18.0.Final/org.optaplanner.core.config.solver.SolverConfig.buildSolver(SolverConfig.java:255)
at org.optaplanner.core#7.18.0.Final/org.optaplanner.core.impl.solver.AbstractSolverFactory.buildSolver(AbstractSolverFactory.java:61)
at io.github.mwkroening.optaplannermodulepathexample/io.github.mwkroening.optaplannermodulepathexample.App.main(App.java:14)
I got a similar exception before when calling SolverFactory.createFromXmlResource.
My workaround for that problem was instead calling:
SolverFactory.createFromXmlInputStream(
App.class.getResourceAsStream("cloudBalancingSolverConfig.xml"));
I looked at PLANNER-883 and psiroky/optaplanner-cloudbalancing-jdk9, but I think a classpath resource was used in that case and not one on the module path.
How could I resolve this issue?
Perhaps I should open an issue instead of posting this here, right?
There's an overloaded method of every SolverFactory.createFrom*(...) that accepts a classloader: SolverFactory.createFrom*(..., classloader). That class loader isn't just used for loading the XML resources, but also the DRL resource.
So try:
SolverFactory.createFromXmlResource("/.../cloudBalancingSolverConfig.xml",
App.class.getClassLoader())
I haven't tested this out yet myself, but if it works, I'd love to see this added to the "jigsaw" section in the manual (PR welcome :).

Running my program leads to java.lang.NoSuchMethodError: scala.Predef$.augmentString(Ljava/lang/String;)Lscala/collection/immutable/StringOps;

I am trying to run an open-source program called LinkedIn Norbert (https://github.com/linkedin/norbert) in Scala 2.8.1. I have added all the jar files so the program compiles, but when I try to run class com.linkedin.norbert.javacompat.network.RunNorbertSetup in examples/src/main/java, I get the following error:
Exception in thread "main" java.lang.NoSuchMethodError: scala.Predef$.augmentString(Ljava/lang/String;)Lscala/collection/immutable/StringOps;
at com.linkedin.norbert.jmx.JMX$.name(JMX.scala:59)
at com.linkedin.norbert.cluster.ClusterClient$$anon$1.<init>(ClusterClient.scala:56)
at com.linkedin.norbert.cluster.ClusterClient$class.$init$(ClusterClient.scala:55)
at com.linkedin.norbert.cluster.zookeeper.ZooKeeperClusterClient.<init>(ZooKeeperClusterClient.scala:22)
at com.linkedin.norbert.cluster.ClusterClient$.apply(ClusterClient.scala:33)
at com.linkedin.norbert.javacompat.cluster.ZooKeeperClusterClient.<init>(ZooKeeperClusterClient.scala:23)
at com.linkedin.norbert.javacompat.cluster.ZooKeeperClusterClient.<init>(ZooKeeperClusterClient.scala:21)
at com.linkedin.norbert.javacompat.network.RunNorbertSetup.configCluster(RunNorbertSetup.java:115)
at com.linkedin.norbert.javacompat.network.RunNorbertSetup.main(RunNorbertSetup.java:21)
What would I need to do to get past this error? I've looked at other Stack Overflow and forum threads, and these have not helped.
FYI: I built the program with SBT.
Thanks so much,
Rebecca
Run a "clean" instead of build. Remove all the build and bin folders from the project directory too.
The solution was to put the relevant class in the classpath.

java.lang.NoClassDefFoundError on android

i am doing an application-email sending without user interaction. so that i got coding from the following link. here i got java.lang.NoClassDefFoundError: com.murali.email.GMailSender. i got this error at
GMailSender sender = new GMailSender("username#gmail.com", "password");
sender.sendMail("This is Subject",
"This is Body",
"user#gmail.com",
"user#yahoo.com");
in the MailSenderActivity Class. i added all external jars in referenced library and no error found at compile time. i spent more time to solve the issue but failed. i know it is possible of duplicate question but the other answers were not used for me. i guess me or eclipse miss some jar or class path for GMailSender class. please help me. i do not know how to solve it.
Make sure the jar sits in a folder named libs, not lib. Then perform "clean" and make sure the jar appears under Android dependencies.
If we get this error on android
java.lang.NoClassDefFoundError: javax.activation.DataHandler referenced from method javax.mail.internet.MimeMessage.updateHeaders
We need to add aditional.jar along with mail.jar and activation.jar. This solved my issue in android when accessing MimeMultipart.

Issue loading quaqua 64 libraries

Every time my app loads I get the following error in the console:
Warning: class ch.randelshofer.quaqua.osx.Application couldn't load library "quaqua64". java.lang.UnsatisfiedLinkError: no quaqua64 in java.library.path
I thought this would be resolved by adding libquaqua64.jnlib to the resource directory that contain my libquaqua.jnlib and quaqua.jar, but after I did this the console error still persists. Anyone experiencing this? Is there something else I need to add?
This might be a silly question, but after you copied in the libquaqua64.jnilib file, did you re-start the JVM?
I took a look at the code in ch/randelshofer/quaqua/osx/OSXApplication.java (Quaqua 5.4.1), and it will not re-try loading the JNI library if it fails the first time.
I just forgot to add it to my build. Adding the libquaqua64.jnilib should do the trick for the warning I described.

Categories

Resources