Right now i've written a simple SWT application using eclipse, and I want to pack it into an executable .jar file so I can give it out to friends and such. But I have the following problems:
-Right now i'm reading files by using their filename in the program, and putting them in the root folder of the eclipse project. That works fine for running in eclipse, but when I export to jar they're not in the jar. Is there a way to put them in the jar and access them in the code?
-I also need the SWT .jar dependencies or whatever its called(the files you need for SWT).
Does anyone know how to do this?
Take a look at ClassLoader.getResourceAsStream() API.
All you need to do is include these files in the with the source code of your project, then to have access to then have a look at this link
Related
I have created an application using the JFreeChart library for use on another machine.
Previously I have developed applications with JFreeChart (using the same libraries) which has worked fine on other machines. The only difference is this machine is running Vista.
Please see below for the run-time exception I am getting:
The class that cannot be found, however, is located in the highlighted jar in the below image showing my imported libraries for the JAR. I have also established that this JAR is included in the manifest for the application. See below image:
So I very much need this to work and have no idea where to look next - or what is causing this problem!
Development machine Java version:
1.7.0_45
Target machine Java version:
1.7.0_45
Thanks in advance.
Check the Class-Path attribute in your JAR's manifest, which should contain entries like this:
Class-Path: lib/jfreechart-1.0.17.jar lib/jcommon-1.0.21.jar …
Also, examine dist/README.TXT in your NetBeans project folder, which should say something like this regarding libraries required by your project:
To run the project from the command line, go to the dist folder and
type the following:
java -jar "CISOnlineMonitor.jar"
To distribute this project, zip up the dist folder (including the lib folder)
and distribute the ZIP file.
This has nothing to do with os i belive.You dont have all the necessery libs within your jar.Try to open a jar and see if you have them in.Fact that you are able to run it on your maschine only proves that.Make executable jar with eclipse or whathever you use.And when it ask you for libs check -Extract required libraries into generated Jar.
if you dont know how to get to that point
File>Export>Java>Runnable Jar File> Runnable JAR File Specification.
Also right click on your project and check Your build path.
RightClick project>Properties>Java Build Path>Libraries
Make sure you have everything correct
EDIT-
As i see you use NetBeans im not sure exactly how to find all this there.Bud it will be very similiar.
So my problem is that I want to export my libgdx desktop project as a runnable .jar file. I already made this work and it is working fine when I open the .jar file from my desktop. However, if I put the .jar file into my Dropbox or in any other directory other than my Desktop, it won't work anymore. The Gameframe opens for like 0.2sec, stays black and closes again immediatly. If I then pull the .jar back to my Desktop it works fine.
I've seen other people ask this before, but none of the answers helped me with my problem.
If you need any more info please let me know!
Try opening the exported JAR in (for example) 7-zip and then adding the 'res' folder manualy. I don't really understand, why Eclipse didn't put the 'res' folder in it. But adding it manualy worked for me.
In eclipse, make sure to right-click the whole project and click export. In library handling, click on "Package required libraries into generated JAR". Then click finish. If that does not work, make sure your build path includes everything.
Can I have access to the .jar file so I can look what is missing inside it?
Most likely you got some additional libraries in your output directory which are missing when you place your jar in dropbox. To confirm this try to start your application via command line. Java will then print a stacktrace with the exception. Example:
java -jar yourjarinthisdirectory.jar
As a side note I would recommend to use maven in the future maven libgdx. In this case you just can say "package" and maven produces complete jar with all libs inside it without further configuration.
I am a Beginner in Java GUI and am using Netbeans 7.3 to use Swing Components, the problem is that all was working fine, but one day when I Clicked Clean & Build and tested the JAR file, it was not executing, I ran it in the Netbeans and It is working fine.
Then I thought that it would be a problem of Build Properties so I compared build properties from a project which was working fine with Jar file but I cannot find any difference, The problem is that when I double-click the JAR file, no Error is shown, I tried many ways like executing it from cmd but It did'nt worked.
Can anyone help me out What could be missing, and one thing more that is I even tried to build my project on another computer through Netbeans but no use.
Try to open your .jar with winrar, and check if the classes are being added to the .jar, If they are not being added probably is a bug from netbeans, I would advise to install netbeans 7.2, and then open your project again, and then regenerate the Jar File. I was having a similar problem with Netbeans 7.3
I think you executing .jar file from different folder - not from where it was created? If so - that means your application can not find some files - pictures, etc. So if you have some files which application uses - create the folder named "lib" and place there your files - but not .jar file. For example:
File file = new File("/images/etc.png");
means that it is placed in lib/images directory. So your .jar file could be run from here:
C:/myTestApplications/myApp.jar
which contains also folder
C:/myTestApplications/lib/images
Try that.
I have a couple files I want to include in my .jar for a game server. The files, a SQLite database and an icon, work properly when and only when I put them in the Eclipse project folder and run it straight from Eclipse. As I want this to be distributable to end-users easily, how can I include these files and use them at runtime? Thanks in advance!
Accessing files from within a jar file can be tricky depending on the code that's using them. If the code is written to open a file from the file system, you have to extract it. If your Java code opens it with Class.getResourceAsStream(), it can be read from within the jar. You mentioned an icon and SQLite. I would guess you need to extract those files. I've done this with the maven-dependency-plugin and unpack when using maven. "jar xf foo.jar icon.gif" works too.
I have created a Java console application using Netbeans. In the Netbeans dist directory I have the class file of the project. Now I need to give the executable files to someone else
who will run them on another PC.
Which file I should send? How can he run them on his PC? Is there any way to create an exe type file?
Both PCs have the JDK installed.
Build a jar file with a main class specified in it.
If he has Java installed and .jar is associated with that, he should be able to just double-click on it.
Alternatively from a command line he'd be able to run:
java -jar program.jar
There are programs around to create executable wrappers around this, but a jar file is a simpler solution in terms of packaging - it's worth trying that to start with.
In additon to Jons answer:
If you have a runnable jar to start with, it is frequently much easier to package it up in an EXE file. If you have the need search Stackoverflow for JSmooth and one-jar.
NetBeans actually answers your question. If I do a Clean/Build, the output says:
To run this application from the command line without Ant, try:
java -jar "insert_your_project_name_here.jar"
theres a handy page about this here:
java tools tutorial
It describes creating jar files a little further down the page
You can go to run menu and select clean and build project or shift+f11 after this go to dist folder in project folder and use .jar file and run that by hint say in over .