There should be something wrong in the way I'm using Eclipse, so let me explain:
I have a folder in the project marked as src, in order to have my configuration being deployed correctly in the classpath when Eclipse compile my project. This works fine since in debugging/running from the IDE works just fine. But if I export the project as a runnable jar file, the config files are embedded in it, and as a result I can't allow the user change the configuration ( I have to remove manually the file from the jar, and put in the current dir in order to achieve the desired behavior). Is there some solution to have this automatic? I did something like this: exported the "Export as a runnable JAR" as an ANT file, then manually modified it to work as I want, but unfortunatelly I have to manually update the file when class path changes. Any suggestion?
In eclipse when you choose export option, you'll see JAR file, Javadoc and Runnable JAR file. Can you try JAR file option, where you can select/deselect the classpath along with the project? It also has an option to select only the class files without resources. Hope that option will be useful.
Related
So my old jar has the libraries exported into it as folders containing the .class files. And my new jar has each library .jar packaged into my jar in a libs folder like my workspace. I need it to export with the former setup. Pictures below explain my issue further.
The reason the packaging changed is because I reinstalled my OS and I don't remember how I had done it before since it's been a while since I last exported a jar for this project, I think what I did last time to get around this issue was I used IntelliJ Idea to export it but I'd much prefer figure this out with Eclipse. There seems to be a setting that does exactly what I want under the Runnable Jar Export wizard but I'm not exporting a runnable jar as it doesn't have a Main method.
old jar picture:
new jar picture:
new jar picture 2:
Yes, a fat JAR can be created via the Runnable JAR file, but not via the JAR file export wizard.
So either
create a dummy main method or
use Ant, Maven, Gradle or something else
to create a fat JAR.
Make sure that you put everything in the source folder that you want to copy to the output folder and include in the JAR.
Switching to IntelliJ Idea allowed me to export in this manner out of the box with default settings. If anyone knows how to get this function in Eclipse feel free to add it.
Edit: as per #howlger's comments I've found a solution where Eclipse works perfectly for my use case.
I have made use of JDBC in one of my projects and it worked flawlessly in Eclipse. I had to add the ojdbc6.jar to Java Build Path to the project BTW. (I have also not included any Class.forName statements in the code since I heard it's optional for JDBC 4 and above)
When I export the project into a jar file, and try to run it, it complains saying "No driver found for JDBC" and so on. I can see that this is because the ojdbc6.jar is missing from the build path now. Can someone please let me know of a solution?
Thanks!
Here is a simple method to export a JAR that uses external jar as libs:
You have to write a manifest file, I named it jar.manifest.
The content in manifest file looks like:
Manifest-Version:1.0
Main-Class:com.enginebai.activity.MyJDBCProgram
Class-Path:lib/ojdbc6.jar lib/some-externam-lib.jar
and make sure to put a blank line under Class-Path, otherwise the JAR won't run. (I have no idea why to put a blank line over there.)
Let's go back to eclipse:
In eclipse, you export your classes and remember to select above manifest file.
Make sure your ojdbc6.jar be in project/lib folder.
Open your terminal and cd to project root directory, enter `java -jar your-jar-name.jar', it should work then.
put ojdbc6.jar on classpath when you run your jar
I have done some research and found a solution that seems to work.
I have exported the project into a "Runnable Jar file" instead of just a "jar file".
This way, I was actually able to repack Jar files from my Build Path into the new Jar file I am exporting my project to. Simple and clean.
You also might want to look at signed Jar files and any other certificates in your existing jar files in the Build Path. They cannot directly be repacked through the export option.
So I have a small Java program with some gui that I have runnning in eclipse just fine. I followed the fatjar tutorial to create the .jar, but when I try to run the .jar the first gui window appears but none of the functions work. When I ran it in cp, clicking the button generated an exception basically saying some of the object types could not be resolved as type, all of which were from import libraries. Has anyone seen this before, I tried using this program jarfix for an issue similar to mine, but nothing.
Please define
When I ran it in cp
The format of the java command should look something like
java -cp /path/to/jars com.main.class
You could start locating the source of the problem by opening the jar file with an archive program (e.g. winzip) and look if the class / type is included in the jar file or not.
If it is there it might be a classpath issue. If it is not there something is wrong with building the jar file. Musn't the included jar file be set in the "Order and Export" tab in the java buildpath dialog?
I am not aware of fatjar. But I faced similar problem, might be somewhere it is linked to your issue.
In my case everything was working when I was running through eclipse, but when I created JAR using Eclipse I faced issue as what you are facing, but in my case it was issue of accessing resources inside JAR File.
So solution was,
Right Click -> Export as -> Jar, here check the box "Add Directory Entries" and then create the JAR. Ans all worked.
Above all, first you should check whether the jar which is generate is correct or not by just open it with winrar or similar tool and see all classes and resources are placed properly.
An alternative would to place all those jar files in the same folder. i.e. if your application jar file is App.jar and other dependencies are A.jar and B.jar. Then drop A.jar and B.jar in the same folder as App.jar.
If you double click on App.jar it will by taking the other dependencies in the same folder by default.
I write a very simple java program with two classes: Business and Main.
I want to create a jar such that if I email it to someone they can:
run the program (i.e. run the jar)
open the jar to view the source code.
the code can run on mac or windows
I have been using IDEs for so long I have forgotten how to do this.
I am using netbeans 7.x
EDIT:
I found the following way on Netbeans:
properties > packaging > exclude from jar file :: delete **/*.java
But when I try to execute the jar using
java -jar mybusiness.jar
it says
no main manifest attribute, in mybusiness.jar
But note that my jar has a main class. Am I missing a manifest file?
You can export a JAR file that includes the source code using Netbeans:
Right click on the project and select properties
Build -> Packaging
Remove Java files from the excluded files. And select build jar after compiling
It will create the jar file that includes the source code if it successfully compiles.
Well surely an IDE can do this too?. Just make some text files and put the source into them and drag them into the ide's. It's java so it should automatically run on all platforms. I am not sure what the problem is here?
This link explains how do u create manifest file and how do you specify your main class in manifest file as its necessary for executing jar.
so my projects have a lot of classpath variables set in the run configuration -> classpath tab in eclipse...
but once I exported the project as a runnable java jar file, none of these classpaths are exported to the jar's manifest file despite having specified this run configuration during the export dialogue....
how do I get eclipse to add these classpath variables to the exported jar's manifest?
besides doing everything manually of course...
You can create your own manifest file and ask eclipse to use it in the "Export->Jar" wizard.
You can also try "Export -> Runnable Jar" in the latest versions of eclipse. I have not tried it, this generally is supposed to include the referenced libraries, so it might include the classpath variables as well.
Eclipse doesn't seem to have an option for creating a manifest, I think the export option is for library files rather than runnable jars. If I remember correctly you have to manually sign+add a manifest to a jar at once? Not sure though. But you would have to manually sign it anyway so not a massive deal.
Check out this post - i believe it answers your question.
Java: export to an .jar file in eclipse