using eclipse, java maven project compiles but gives error when running - java

There are a large number of answers to the error I'm getting, but each of the solutions that I've tried (that I understand at least) have not solved my problem.
My project layout in Eclipse looks like this:
MavenProject1
src
resources
etc.
MavenProject2
src
resources
etc.
MavenProjectX
src
resources
etc.
Each project compiles and runs fine. But what I'm trying to do is use one of the projects, say some of the code in MavenProject2, in MavenProject1. I've updated the build path and information so that I can import MavenProject2 into MavenProject1 and reference the methods I want.
The reference to the MavenProject2 method in MavenProject1 enumerates and doesn't give an error in the editor (i.e. com.MavenProject2.Method()), and MavenProject1 compiles. But when I try to run it, I get the error below. I've tried fixing the classpath (as I understand it, which could be wrong), I've tried adding the project, external jar, just about every option, in the run configuration for the project, but I keep hitting the same error on line in MavenProject1 that calls the method in MavenProject2.
[WARNING] 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.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:297) at
> java.lang.Thread.run(Unknown Source) Caused by:
> java.lang.NoClassDefFoundError: com/MavenProject2/Method_From2 at
> com.MP1.MP1.main(CAPI.java:154) ... 6 more Caused by:
> java.lang.ClassNotFoundException: com.MavenProject2.Method_From2

When you first build the projects that your current project depends on, make sure they are stored in your local maven repository. Then when you add those as dependencies to your project's pom.xml this should hopefully resolve it for the build.

Related

Application runs fine in Intellij but doesn't work when packaged into an exe file

I have built a very simple command line application using picocli and I would like to package it into an executable file. I first packaged it into a jar file using Maven package, and then used Launch4J as recommended by several users on SO to obtain an executable. However, it throws the NoClassDefFound error, when I run Application.exe in the command prompt.
> Exception in thread "main" java.lang.NoClassDefFoundError:
> picocli/CommandLine
> at mycli.HelloCli.main(HelloCli.java:48)
> Caused by: java.lang.ClassNotFoundException: picocli.CommandLine
> at java.net.URLClassLoader.findClass(Unknown Source)
> at java.lang.ClassLoader.loadClass(Unknown Source)
> at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
> at java.lang.ClassLoader.loadClass(Unknown Source)
> ... 1 more
The application works perfectly well when executed in IntelliJ. I tried adding the custom jar file I created by looking at answers similar to these but it doesn't seem to help. I also tried changing the folder of execution but that doesn't solve the error either.
From several answers I searched across SO, I realized that the picocli jar needed to be on the class path. So I added the "Main class" as the one containing my main method, and specified the "Classpath" to the jar file created by building an artifact out of all the dependencies present, while configuring Launch4J.
For details on how to build an artifact jar out of a project's dependencies, visit this answer if you use Eclipse, or this answer if you use IntelliJ.)

ClassNotFoundException when trying to run a previously correctly running application

I have a java project 'eartquake' in Eclipse containing several packages. The project icon contains a red exclamation mark. I cannot find out what it means. When trying to run any package of this project I get the ClassNotFoundException error. When I run for example the AppQuake applet from package earthquake I get the error shown below. The .class file is in the build directory of the package. It has run before. Other applets from other projects still do run. What am I doing wrong?
load: class earthquake.AppQuake.class not found.
java.lang.ClassNotFoundException: earthquake.AppQuake.class
at sun.applet.AppletClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.applet.AppletClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.applet.AppletClassLoader.loadCode(Unknown Source)
at sun.applet.AppletPanel.createApplet(Unknown Source)
at sun.applet.AppletPanel.runLoader(Unknown Source)
at sun.applet.AppletPanel.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
The red exclamation mark means that the IDE is notifying that there are build errors in your project: see documentation
On Stackoverflow you can find someone who had the same issue here and a possible solution here in case you changed the project structure and left some dead dependencies.
Your project is probably misconfigured, or missing some dependencies in the classpath.

I want to use Git in console and Intellij as my

I want to work with Intellij but without it's git plugin.
I worked with eclipse + git in console and never had a problem. Just push/pull/commit/add etc. and opened the files with eclipse.
Now that i've switched to Intellij, i get many errors, no matter how i open/import the pulled files to Intellij.
Most of the times it look like this:
Exception in thread "main" java.lang.NullPointerException
at Test.main(Test.java:18)
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.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Or even this:
Exception in thread "main" java.io.FileNotFoundException: C:\Users\John\Desktop\Test\Program\GitFiles\GG\DC\out\production\DC\level1.txt (The system cannot find the specified path.)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:195)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at java.io.FileReader.<init>(FileReader.java:72)
at model.Map.mapImport(Map.java:35)
at model.Map.<init>(Map.java:26)
at model.Model.initialize(Model.java:120)
at model.Model.<init>(Model.java:37)
at Test.main(Test.java:27)
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.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
However, is there any way to absolutely correcly import Project Files into Intellij that have been correctly pulled via Git?
I even have the suspicion that the Git-Plugin from Intellij migtht sabotage everything, because it notices there is Git on the folder i am working with.
I've wasted many hours on this problem, i would be tremendously happy about help!
Greetings,
John
If you don't want to use Git inside IntelliJ you should disable it. Go to Configure (In welcome menu) > Plugins > Git Integration , and deselect it. Looks like this
Hope it helped you.
That finally did it. i have played around before with the outputpath settings, but there is one configuration i had missed:
1. Project Settings - Project - Output path (choose EXTERNAL output ( not into Project files itself), wherever else you want)
2.Project Settings - Modules - Paths - Inherit project compile output path.

How to use xstream fro xml serialization in eclipse

I am having some trouble figuring out how to download and use xstream in eclipse. When I download the binary distribution from the website, I get a zip file. Where should I extract it, and how do I use it?
Edit:
OK, I have added it to my build path, but now when I try to use XStream to serialize an ArrayList<Earmark> where Earmark is a class that I have defined, I get the following error message:
Exception in thread "main" java.lang.NoClassDefFoundError: org/xmlpull/v1/XmlPullParserException
at com.thoughtworks.xstream.XStream.<init>(XStream.java:334)
at examples.TestEarmarkExtractor.SerializeEarmarks(TestEarmarkExtractor.java:19)
at examples.TestEarmarkExtractor.main(TestEarmarkExtractor.java:48)
Caused by: java.lang.ClassNotFoundException: org.xmlpull.v1.XmlPullParserException
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 3 more
Update: The error is occurring before I try the serialization. It occurs on the line
XStream xs = new XStream();
Extract the zip file to a directory (e.g. C:\Program Files\XStream)
In eclipse, right-click on your project and select Build Path > Configure Build Path...
Select Libraries tab
Select Add External Jars..
Navigate to the lib folder of your XStream download and select the jar(s) to be added
Select Open
Select OK
And here's the Two Minute Tutorial on how to use XStream.
Got this exception, then I download kxml2-min-2.3.0.jar and add it as a library and now it works (using xstream 1.4.4.jar)
You need to unzip the .zip file to get at the .jar file, which you put on your classpath for your project in eclipse (Google if you don't know how to add third party library .jars to your project classpath in Eclipse).
Once it is added to your project's classpath, you use the classes contained within it as you would any other classes, and the documentation on the XStream website tells you how to use them.
Good choice by the way, XStream is great!
You can add this library:
<dependency>
<groupId>xmlpull</groupId>
<artifactId>xmlpull</artifactId>
<version>1.1.3.1</version>
</dependency>
I had added the two libraries: dom4j-2.0.0-ALPHA-2.jar and xstream-1.4.7.jar and the same issue were displayed. After adding the kxml2-min-2.3.0.jar the issue is not happening.

Why Exception in thread "main" java.lang.NoClassDefFoundError:?

I run my software through Eclipse. Yesterday everything was fine. I made not changes to the code but today, when I am trying to run it again I get the following error messages:
Exception in thread "main" java.lang.NoClassDefFoundError: coloredtrails/CTListener
at test.DemoPlayer1.createAndShowGUI(DemoPlayer1.java:23)
at test.DemoPlayer1.main(DemoPlayer1.java:39)
Caused by: java.lang.ClassNotFoundException: coloredtrails.CTListener
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 2 more
Why it does not see the class? What could be the reason of that? How can I resolve the problem?
A NoClassDefFoundError (almost) always means that your classpath is wrong. Make sure that your classpath includes the base directory of the coloredtrails package. (Ofcourse, also make sure that the file coloredtrails\CTListener.class actually exists).
When running from the command line:
You can set the classpath by setting the CLASSPATH environment variable, or by specifying it with the -cp or -classpath option on the command line when you run your program. For example:
java -cp C:\MyProject\classes coloredtrails.CTListener
edit - Looking at the stack trace and seeing URLClassLoader in there makes me think that you are trying to run a Java applet. To learn how to correctly deploy applets, so that all classes the applet needs can be found, see this tutorial: Deploying an Applet.
Sometimes, my Eclipse (Indigo on MacOSX) does that, expecially if I do changes (removing files, moving them around) to the project structure on the filsystem directly.
Basically, eclipse cannot find the source folder anymore, so he doesn't compile the source but tries to run it anyway (all this without a warning or a reference to the problem).
To fix it, remove the source folder from the build path (=right click on the src folder under the project in the package explorer, then choose "Build-path->Remove from Build-path". Then, add it again (=right click on the folder under the project in the package explorer and choose "Add to build-path"). This makes the src folder "visible" to the compiler again and fixes the problem.

Categories