Eclipse create java executable with external libraries - java

I've seen this topic in this forum but it I need a more basic explanation on how to do this.
I've done a program in Java with some external libraries (LWJGL and Slick).
So this is what I've done and my program won't start anyway, tell me where I've done wrong.
I have Eclipse 3.7.1
My project is opened in Eclipse and runs well in Eclipse
I click File -> Export
I select Java -> Runnable JAR file
Here I don't know what to choose in Launch configuration, when I click the dropdown I get the option to choose my main class so I do that.
I select an export destination
I select the option "Package required libraries into generated JAR" under Library Handling
I don't know what ANT script is so I don't use that
I click Finish
I copy my images-folder to the same location as the generate JAR-file
I try to start the JAR-file, something loads in the background but nothing happens, no window shows up, nothing.
I check the Task manager in windows and sees that a javaw.exe is running
What did I miss?
My program uses images for graphics like this:
image = new Image("images/filname.png");
I wonder if I need to change the paths before exporting or the method to load these?
Thanks!

If you want to get things from inside a jar file you need to have them in your classpath and access them as resources.
The constructor you use, refers to a physical file which cannot peek inside a jar-file.
(EDIT) Also note that you have no guarantee where the current working directory is. Hence any relative references may break, as you see.

Read this to learn how to use JarSplice to export your Eclipse project to a runnable jar-file. Regarding images, you can put them in the same directory as a class file (in the jar) and then writeSampleClass.class.getResourceAsStream("image.png")to retrieve an InputStream of the image. Then you can load it however you like.

I had the same problem and I was able to fix it. All I did was copy the data folder (which contains my resources) into the *.jar file. You can do this for example with WinRAR.

Related

How to export my program into a runnable desktop file?

first time posting here, java beginner.
I made a basic calculator that receives user input. Is there a way to export my program to make a runnable desktop file?
My IDE is IntelliJ.
Thanks in advance!!
Assuming you are building a JavaFX app:
The easiest way to deploy your app is to go to File>Project Structure>Artifacts.
Add your available elements (if you have any extra images etc) into your output root, and click on your jar file. At the bottom of the window you'll see options to either create a manifest file or modify an existing one.
The manifest file describes the first class to load in your program (the starting point) as well as the locations of any third party libraries you may have included in your program.
You will also have a JavaFX tab that you can use to set some initial parameters for your app, such as the title, version, and whether you want to deploy any native bundles (eg: .exe for Windows, .deb for Ubuntu etc)
Once you have configured the important parameters you want, save your settings (or just select any field and click enter) and go to Build>Build Artifacts>Action: Build
IntelliJ should generate an executable .jar file and any native bundles you selected. The native bundles can just be double clicked, and the jar file can be run using a JRE.
On blog.jetbrains.com they show the steps with some screenshots.
Note that this entire process is called deploying your app and there are a variety of tools and methods to do it. I just described what I believe is the simplest way.

How do I add LWJGL 3.1.1 to netbeans to use in a Java project?

I want to use the Light Weight Java Gaming Library(LWJGL) to my Netbeans so I can use it in my Java application. The only videos that I can find show the zip file that they downloaded with separate src and doc folders inside of it. The zip file that I download has everything in one directory. I went to lwjgl.org/download and clicked on Stable and then Generate Bundle. What am I missing?
I had the same problem recently.
So, to begin you want to go to Tools in the context menu and select Libraries (as shown). Next you can add a new library and name it e.g. LWJGL-3.1.1 confirm with ok. You can find 3 tabs in the current window Classpath, Sources and Javadoc. There you add your jar files accordingly (in the downloaded .zip file you find .jar files with different names like lwjgl-{whatever}-sources.jar or lwjgl-{whatever}-javadocs.jar) make sure you put them in the right place. You have to repeat this process for all of the jar files you want. The javadoc files are not required but recommended. Make sure you also collect all the native .dll files and merge them in a folder called \natives. You find them in these jar files that are called like: lwjgl-{whatever}-{your-OS}.jar.
Once you have finished the setup for your library right click on your current project and choose Add Library.... In the window that pops up you scroll down until you find your library that you have just created and you are almost done now.
Last but not least go to the project settings. Select Run and make sure that you set the classpath in VM options to something like in the image: -Djava.library.path="C:\java_workspace\LWJGL Library 3.1\natives. Now this classpath tells netbeans where your native files are located. Your \natives folder that you should have created in the beginning is where this path should lead to. That's it. This is all you have to do for a setup without the use of maven, gradle, ...
You can test if it is working with the code provided by LWJGL HelloWorld example.
I hope this solved your problem.
Best regards.

Using Java to change Windows 7 desktop background from inside a runnable jar

I'm using code from here to change my desktop background to a set picture. This code works perfectly in Eclipse, but when I package the application to a runnable jar, it doesn't do anything. Running
java -jar DesktopChanger.jar
from the command line doesn't result in any errors. It just prints out a job completed message (sysout) and ends, but the wallpaper didn't change.
I imported the jna libraries jna-platform-4.1.0.jar and jna-4.1.0.jar from here. The only difference between my code and the other question is a sysout, a messageDialog to say the job finished, and the location of the picture:
String path = "src/background/changer/picture.jpg";
Finally, in Eclipse I right click on the project
hit "Export"
select "Runnable JAR File"
select "Package required libraries into generated JAR"
and finish.
I've even messed with the Manifest.mf file since Eclipse sets "Main-Class:" to it's own main, using "Rsrc-Main-Class" for the actual one. So I deleted "Rsrc-Main-Class" and put my main into "Main-Class" but again nothing works.
Do I also need to package the native code or dll files?
Your issue is the path of the image. The path you used is inside of your jar file, and as such windows can't locate it. Thus, it just ignores the request.
Change it to something like this:
String path = "C:\\picture.jpg";
and put the picture in that location.

What's the best way of storing and extracting files inside the .jar archive?

I am looking for a way to store files inside the jar (and extract them), but it must work when running/debugging from Eclipse as well.
explanation:
Storing files as in images that I want to use for an icon of a Frame. I hope it's clear now.
I still think that the question is a bit unclear.
What is it that you want to do? You can create and read from jar-files using JarInputStream/JarOutputStream, and you can also read files from jar-files that are on the classpath using Class.getResourceAsStream(String name)
I don not get your question clearly.
Eclipse can export runnable jar files , right click on the project name and choose export. Then from the dialog choose runnable jar file under java category.
Creating JAR file using Eclipse IDE is pretty much easy.
Follow the simple steps.
1. Right click on your project, which you want to create a JAR file.
2. Select Export from the context menu.
Eclipse understands Ant build files. Write a build file that (compiles &) Jar's the code at the same time and this becomes a non-issue.

Exporting Images with JAR in Eclipse (Java)

I've been working on a little project that requires external images for display. I'm not all that familiar with how to use Eclipse and this is my first time attempting to export a completed project so I can share it with others. Right now, it seems the only way I can get my images to show up is if I assign a specific folder on my hard drive and have the image paths in the code go to that.
I'm looking for a way to export the images as part of my JAR or as part of the same package so when I go to send this program to other people, I don't have to send them a separate archived folder of images. I'd also be interested in learning what I need to do to have my code reference the images within that package so they'll work without an external folder.
I have read about some kind of package system within Eclipse, but have thus far had no luck in figuring out how to use it. Could use some explicating!
Thanks in advance to anyone willing to give me their two cents.
Something I would have found useful with this answer is the following: make sure you put your images/files in the same eclipse folder (or sub-folder below) as your source code. I created a folder "images_ignored" using eclipse, added it to the build path but still it refused to be included in my JAR file (when creating an executable JAR).
Just drag the images folder into your Eclipse project, then choose to "Copy New Folder" or "Copy File and Folder" depending on Eclipse version, and then right click on the image folder (in Eclipse) and --> build path "use as source folder".
you might need to load them as class path resources if they are within a jar. see: getClass().getClassLoader().getResourceAsStream(...)
Use getResource() to load the images:
ImageIcon qmarkIcon = new ImageIcon(getClass().getResource("images/mark.gif"));
If you're using JDK 1.7 or JDK 1.8, you might want to use the NIO.2 API.
for (FileSystemProvider provider : FileSystemProvider.installedProviders()) {
if ("jar".equals(provider.getScheme()))
return provider.newFileSystem((new File(Start.class
.getProtectionDomain().getCodeSource().getLocation().toURI()))
.toPath(), new HashMap<String, Object>());
}
If you enter this code into a method that returns a java.nio.file.FileSystem, you can call the method to get the FileSystem for the JAR file.
To get the path to access the files inside your JAR file, you can use the following method, which then allows you to read the files however you may want.
fileSystem.getPath("/images/image.gif")
If you would like to be able to run this in Eclipse, make sure you surround the call to the method with a try/catch IOException and assign to your FileSystem object the following.
new File(Start.class.getProtectionDomain().getCodeSource().getLocation().toURI())
.toPath().toString();
This will allow you to run your program whether it's compressed into a JAR file or not.
I recommend you get used to using NIO.2, since it is a very powerful API.
If you add a folder to build path you can retrieve the images either in eclipse and when you exported it in jar file, just remember to don't reference the image with the path like img/myImage.gif but only myImage.gif !

Categories

Resources