I am writing a plugin which will be loaded by another application of course, and I want to use JSoup for some web pulling. However, when I bundle the jar and run the application, I get:
NoClassDefFoundError: org/jsoup/Jsoup
I have jsoup in my classpath just as all my other dependencies are. Jsoup however, will not be on the hosts machine. I have also tried bundling the project jar with jsoup in a lib folder within the jar itself and referencing it from there, and I still had no luck. How can I get Jsoup loaded on the JVM in my project?
Edit: I can run Jsoup fine in eclipse, just not when it is bundled in a jar
Edit2: Why is it that I can have some dependencies work without having it loaded on the hosts machine such as google's gag project? why is it that I need jsoup loaded on the hosts machine? I know I have a misunderstanding of how the JVM works :/
Jsoup should be on the host machine if your code is going to use it. Why not place it with your own jar file, and set your jar's manifest so that it knows where to find the Jsoup jars? This question has nothing to do with Jsoup per se and all to do with using external libraries contained in jar files, but as it happens I use Jsoup in several of my final projects, and they work just great when used as most jar files should be used, as libraries contained on the host machine.
Edit
You state:
That's what I tried to do. I have decompiled the jar and Jsoup is bundled within in jar format. However, I have never messed with the manifest before. How would I set the manifest to look for it in the projects jar?
If the Jsoup jar file is in the same directory as your jar file, simply refer to it directly in the manifest's class-path:
Manifest-Version: 1.0
Class-Path: jsoup-1.6.1.jar
Main-Class: mvc.main.SimpleImgScraperMain
The details on use of the Class-Path property within manifest files can be found here: Adding Classes to the JAR File's Classpath
Related
In eclipse tree or any eclipse based application.
I can see that inside the plugins folder.
most of plugins are in the jar format while some of them are in folders
and this folder is just the jar extract .
why someone could do this ? what is the benefit of putting a plugin as extracted jar rather than only jar file ?
You can do this to embed jars in the plugin that are used in classpath because yo can have issues with nested jars in classpath.
I think that you can also access resources differently. And it can be an easy (and dirty?) way to allow some customizations by overriding some resources such as images.
Let's say a plugin will need to execute an embedded OS dependent script on its start-up (ex. ".bat" script). If this plugin is installed as a jar, it will need to :
get an InsputStream of the embedded script
ensure it can write it somewhere (temp folder usually)
run the script
at the next start-up; check that the script has not already been extracted
...
Or, if this plugin is extracted as folder :
run the script.
I made a simple standard-lone java Application using Spring,Apache Camel,Activemq for processing messages.
Note: My Application don't have any GUI.
My project structure is in the following way.
SACLib folder have nearly 70 external jars(all Spring,Camel and Activemq corresponding jars).
It's working fine in Eclipse. SO Now We want to deploy into Jar file.I tried in Eclipse,But I didn't seen Rod1,Rod2,Copy1 and SACLib folders in my Jarfile.
after Deploying Jar, If I run FirstConsumer.java it runs Rod1-->ThMapInfratab1-2.exe file. For this I mention Real paths of .exe file.
How can I make Jar file with including all my folders.
Thanks
Well, this is a kind of work that is typically done with build automation tools like Apache Ant, Maven or Gradle, so you can investigate there if you want to make this happen automatically next time.
But, if you want to do it manually...
First, you project needs a META-INF folder where you will place a file called a MANIFEST.
That manifest contains a Main-Class entry pointing to you main class. You can read about this in the Java Tutorial: Setting Application's Entry Point.
But it can also contain a Class-Path entry, pointing to all other jars required by your application and that should be loaded by the executable jar.
You can read about it the Java Tutorial: Adding Classes to your Jar Class Path.
If you are building your executable jar with Eclipse, it will let you choose the MANIFEST file that you want to use during the creation process.
Now, if you want to use build automation tools, there are other answers here that explain how to do it:
Creating a bundle jar with ant
How to create executable jar with dependencies with Maven
How to export an executable jar in Gradle
simply using ant download it , and then make a build.xml file and put it
Here's an simple example of an ant target that will create a jar (named test.jar) that includes all jar files under the lib directory. Maybe this will solve your problem?
for using apache ant, see this
http://ant.apache.org/manual/using.html
HI I've found myself referencing a JAR archive I made awhile ago in my code. But the JAR I made has references to the JSoup JAR file, which I just discovered is not automatically added as I had assumed it was (stupid assumption). So after looking around Stack-Overflow it seemed that by simply adding some Class-Path information to a custom Manifest I could solve my problem...But I still get a ClassDef error when I try and reference my JAR from the other project.
Manifest-Version: 1.0
Class-Path: "C:\Users\ethan\Documents\ACTUAL My Documents\Libraries\Java Libraries\jsoup\jsoup- 1.6.3.jar"
SO I'm not sure if I wrote the manifest wrong. (I tried with and without quotes, neither worked). I don't have ANY experience in writing manifests, my knowledge came from this post on stackoverflow
Eclipse: How to build an executable jar with external jar?
from McDowell's post.
I've seen a lot of posts suggest using Maven, or a third party application. But it seems like there should be some simpler way to make this happen in Eclipse without installing anything new (as this often can bring on new headaches).
The Class-Path in the Manifest is only used when you run an executable jar. It won't be used for jar files used as libraries. You will have to tell your main JAR file about it, too.
Well I have 2 .jar files. The main jar file is the jar file for my whole project and the other .jar file being the MySql JDBC Connector.
Well basically whats happening right now is that when I build the project I have the one main .jar file with everything but the MySql JDBC Connector .jar file is inside the main jar file when it builds in NetBeans.
Now when I am just running the project from within NetBeans the MySql JDBC driver can be found inside the src/com/game/mysql folder that I have it in. But when I build the project the Java application cannot locate the JDBC driver from within the main jar file.
When I open the main jar file with WinRar I can see that the JDBC jar file is still in its /com/game/mysql/ folder. But why cant the Java application access it?
I have heard that nested .jar files are not supported in Java so Im thinking this might be the reason although Im not sure if thats true. Is there a way that I can make it so that the application can find the JDBC .jar file within the main jar file?
Also I have done the thing in NetBeans where you add the .jar file through right clicking project -> properties -> Library -> Add Folder/Jar. Thats what makes it work in the NetBeans run but still not the App build.
I have heard that nested .jar files are not supported in Java
More precise, classes in a JAR file which is packaged as a child JAR inside a main JAR are indeed by default invisible to classes in the main JAR.
You have basically 2 options:
Ship your application with 2 loose JARs: your.jar and mysql.jar and define the relative path to the mysql.jar in the Class-Path entry of the MANIFEST.MF file of your.jar.
Class-Path: mysql.jar
When you put both JARs in the same folder and execute your.jar by java -jar your.jar, then it will work.
Let your IDE repackage all loose classes of mysql.jar inside your.jar or add a special classloader which preloads the classes of any embedded JARs. Since I don't do Netbeans, I can't tell whether it supports it and if so, how to do it. In Eclipse, however, this is definitely possible. See also this answer.
We used an external library, specifically jmf.jar (Java Media Framework), for our java application which relies on images captured from the webcam. However, when we tried to package the application into a jar file, the application runs but once we try to access/open the webcam nothing happens.
We are using Eclipse and we used its export feature to create the jar file.
Does anybody know how to solve this?
Did you think to include jmf in the manifest of your jar ? See this link for more explanations here
You don't just need to export your project into a jar file; you need to create an "executable" jar file. This means that the manifest of the jar file is edited appropriately so that the classpath includes all required dependencies and the main class is set correctly.
In recent versions of Eclipse you can use the Runnable JAR File Export Wizard. Have also a look at the Fat Jar Eclipse Plug-in.
If you are using JMF you’ll need to include jmf.jar and jmf.properties in the same directory as the executable jar