How to use xstream fro xml serialization in eclipse - java

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.

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.)

Where can I add javax.media.jai Jar package in Java?

I am getting class not found(javax.media.jai.JAI) exception while running my code. I have downloaded(https://www.oracle.com/java/technologies/java-archive-downloads-java-client-downloads.html#jai-1_1_2_01-oth-JPR) the JAI jar and added in Project lib folder but not working getting same error again. where can I add this jar. Please advise.
Note : The is old servlet project structure there is no Maven features.
Error Log:
java.lang.ClassNotFoundException: javax.media.jai.JAI
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1365)
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1188)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
enter image description here
You can go to build path and add ur external jars by downloading it online.

How to identify the dependent libraries I need

I am building a little something in Java that uses Wolfram|Alpha to come up with integrals. I downloaded the Wolfram API and it comes with a sample program that you can use to figure stuff out. Here's that at GitHub.
There's a portion in the comments that says:
* To compile or run this program you will need the following dependent
* libraries on your classpath (including WolframAlpha.jar, of course):
*
* commons-codec-1.3.jar
* httpclient-4.0.1.jar
* httpcore-4.0.1.jar
* commons-logging.jar
... and I have no idea how to do this. I'm using this project as a way to learn Java, so I am skipping a bunch of sleep-inducing fundamentals.
Are these libraries available in the vanilla Java install, or do I have to go fetch them from elsewhere? I want to avoid loading up my system with redundant stuff.
When I try to run the script without loading these libraries it ends in an error, predictably. Here's the error message:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/http/conn/scheme/SocketFactory
at com.wolfram.alpha.net.HttpProviderFactory.getDefaultHttpProvider(HttpProviderFactory.java:18)
at com.wolfram.alpha.WAEngine.<init>(WAEngine.java:36)
at simpleSample.main(simpleSample.java:58)
Caused by: java.lang.ClassNotFoundException: org.apache.http.conn.scheme.SocketFactory
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
I am using Eclipse as my IDE. I don't think it matters here, but it might.
#dimplex has already answered. These are releases of several Apache projects. Next time you meet these problems, try:
Google:
Like:
Google commons-codec-1.3.jar
Google httpclient-4.0.1.jar
Google httpcore-4.0.1.jar
Google commons-logging.jar
The first result for EVERY search above is a couple clicks away from the download you're looking for.
Maven Central Search:
http://search.maven.org
(I'm not copying every link, just search for the Jar file names.)
This one is even more direct, but I'd recommend the Google way, as you can get a glimpse of the project page.
You have to upgrade the httpclient.jar to version 4.1.
As you can see in the javadoc theses class available from this version
These are all (old) Apache Software Foundation libraries. The download locations are as follows:
commons-codec-1.3.jar
httpclient-4.0.1.jar
commons-logging.jar
httpcore-4.0.1.jar

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.

Getting a weird ClassNotDefFoundException on runtime with GWT

I am deploying a web application based on GWT. When I compile, I don't get any errors, but in runtime, I get this one (not complete stack trace):
java.lang.NoClassDefFoundError: org/xmlpull/v1/XmlPullParserException
at org.gvsig.remoteClient.sos.SOSClient.<init>(SOSClient.java:47)
at com.uji.project.server.ConnectServiceImpl.makeConnection(ConnectServiceImpl.java:18)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
Caused by:
Caused by: java.lang.ClassNotFoundException: org.xmlpull.v1.XmlPullParserException
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
I have read on this website that that is probably caused if one or more classes are not available on running time but, as seen, the error is caused by some problem with the XML parser, but I have the XML parser included in my lib folder. In detail, in the project's lib folder, it is possible to find 3 JAR files: gwt-maps.jar, gwt-servlet.jar, and kxml2.jar*.
If I open "kxml2.jar" file I can see folder org that within has xmlpull folder, that within has v1 folder and finally XmlPullParserException.class.
So...why on runtime do I not have this class available if it is included on the lib folder?
In appengine classes can only access to the classesof the JRE which are in this whitelist. If this jar is using another class of the JRE it won't be included in runtime.
Here you can read that "XML processing APIs including DOM, SAX, and XSLT" are compatible with appengine. Maybe xmlpullparser is not compatible, but I'm not sure.
Finally a colleague told me the solution to this runtime error! :D
When a GWT application is deployed, many files are copied under a small file structure (pending from ) that contains all classes, libraries and other files that are needed on runtime.
For some reason, if you don't tell on purpose Eclipse to copy (on compile/deploy time) a specific library into that PATH, the application will not find on runtime that library and will raise an error.
Solution (for me) was as simple as copying by hand that library into that folder: In my case was copying "kxml2.jar" under "C:\gwt-2.1.0\SOSProject\war\WEB-INF\lib". I restarted the application and just worked. :D
I hope this patch/solution will help others to solve this annoying problem under GWT!
Try this
Window->Preferences->Java Build Path->Add Jars
Navigate to your project/lib, select kxml2.jar Hit OK.
In the end, what I did was to start again from the beginning: In the same workspace I had different projects and maybe there were, let's say, interferences between them.
So, I unzipped again the GWT SDK, start a new project, copied the original source code on the new project, deployed it and on runtime, now I am not getting that weird ClassNotDefFound error. Now I am getting another one, but that is another issue! Thanks to all.

Categories