Add resources into a jar upon building - java

I want to add DLL's, images, textfiles etc to my project as resources so when I export it, the jar contains the resources so they can be used. I am using eclipse.
Problem is I have no idea how to add it. I've tried adding DLLs/pics to the src folder in the project, but when I export the jar, it is not located there
I've looked at How to make a JAR file that includes DLL files? but it only explains how to extract it, not how to add it to the project and build.
EDIT: I am using an applet to open the jar by the way, sorry for missing it!
Cheers

How are you opening the file in java?
Class.getResourceAsStream(name)?
If you are packaging the code in a jar, then you need to use that command. (as opposed to new File(name), which will get the file in the same directory as your jar)
If the file is not physically in your jar, you can check by changing .jar to .zip and extracting it, then check out this doc http://docs.oracle.com/javase/1.5.0/docs/tooldocs/windows/javac.html

Usually in an eclipse project, the src folder is the wrong place to put non-sourcecode-content.
You should try moving to maven as your build system, as it is highly customizable and provides you with folders inside your project for exactly that purpose. (src/main/resources)

Related

How to put a java project in an exe or jar?

I want to put my java Project in a .jar or in an .exe. I can not find a solution for my problem.
That is my project structure:
project
1.1. pom.xml
1.2. .idea
1.2.1. libaries
1.2.1.1. many maven dependency files
1.3. .GUI
1.3.1. .idea
1.3.1.1. libaries
1.3.1.1.1. many maven dependency file
1.3.2. src
1.3.2.1. main
1.3.2.1.1. java
1.3.2.1.1.1. .java files
1.3.2.1.2. recources
1.3.2.1.2.1. .fxml files
1.3.2.1.2.2. CSSStylesheets
1.3.2.1.2.2. .css files
I follow instructions on youtube for creating a .jar in Intellij (https://www.youtube.com/watch?v=ZlyTn8PZ3Fc) but the .jar was not executable. It just print this Display parameters as parsed by Maven (in canonical form) and comparison result: to the console after running java -jar file.jar. (After the double dot there does not come more information.)
Next I found another youtube video that shows me it does not work like in the first video because of the maven dependencies (https://www.youtube.com/watch?v=HGHu-SzL-5E). In this video I do not know what to do in 5:39. He copies something but I do not know from where that is. I tried to put the same files in there like he did but it does not work. There comes a ClassNotFoundError that it can not be found the .fxml files. Then I do not write them in the command but all other .java files but it was the same error.
On this (https://introcs.cs.princeton.edu/java/85application/jar/jar.html) site I had my next try. I follow the instructions like this: I dreated a .mf file and put all .fxml and .java and .class files in the command but it can not be found the .class files so I put them out of the command. And then ist comes a ClassNotFoundError. It can not find the Main class. Intellij mark it in the .mf file red as well and I do not know why.
Then I follow the instructions of this (https://cwiki.apache.org/confluence/display/MAVEN/Tutorial%3A+Build+a+JAR+file+with+Maven+in+5+minutes) site but there an empty .jar file was created.
Now I do not know what I did wrong or what I miss so I hope someone can help me.

Problems exporting, File not loading from project

What's going on is that I have my images and .wav files in a a project folder and it's running fine in eclipse, however once I export it into a executable jar file it stops loading images. I've tried Extracting libraries into jar file and I've tried packaging them in a jar file. What do I do? I'm panicking because I need to get this ready by tomorrow! I know I have the path right, it's starts the code, but stops after a little.
I get this error:
JAR export finished with warnings. See details for additional information.
Exported with compile warnings:
AdventureQuestv4.0.2/src/adventureQuestv/AdventureQuestMain.java
Here's the code I use to load an image
URL GiantT2 = AdventureQuestBetaMain.class.getResource("GiantT.png");
giantT = ImageIO.read(GiantT2);
I know the images are in the file, it's just that they're not loading.
check out this post.May be you are also having the same issue.
How to export jar with images on Eclipse?
Use fat jar eclipse plugin. Select all resources like .wav and image files etc while exporting.
Before exporting resolve all Java compiler errors and warnings(to the maximum extent).
While exporting the jar file with images in your eclipse ,
You must check these in the Jar Export dialog box,
1.Export generated class files and resources
2.Export Java source file and resources
Options:
Compress the content of the jar file
Add directory entries
please delete the existing workspace and reimport the source code
i hope this may resolve issue

How to use *.jar file in a Java project?

I'm a complete novice to Java, and I'm stuck in trying to use an external jar library in a Java project I'm working on.
The library is Twitter4J (http://twitter4j.org/), a complex group of files which I can't understand how to include. I'm using Eclipse on Windows. I understood the meaning of CLASSPATH enviroment variable (or well, I think I did), but I can't understand how to link it to the Eclipse enviroment.
This blog post seemed to be what I was looking for: http://jasperpeilee.wordpress.com/2012/01/25/how-to-set-up-twitter4j-quickly-in-your-java-project/
But:
Add twitter4j-core-2.2.5.jar into project for general use.
1) Where should I copy the core file? In the /bin/ folder? Or in a completly unrelated /lib/ folder, as this question in SO stated?
Copy Twitter API property file into your project/bin directory.
2) What is the Twitter API property file?
3) How can I include JavaDoc jar files too? Is it possibile to get them into Elipse?
Any kind of help, even just a link, will be very appreciated!
You probably have a project folder for your Eclipse project. Go ahead and make a lib folder inside of your project folder, in the same directory as src and bin. There's nothing special about the lib folder specifically, it's just a convenient name. Copy the .jar file into your newly created folder. Back in Eclipse, right click your project and click Refresh. The lib folder should pop up, including your JAR! Right click the JAR file and go to Build Path --> Add To Build Path. At this point, any class in the project should be able to use Twitter4J features.
As for the What question, the Twitter API property file contains configuration that is necessary for the Twitter jar to function. The file should probably be located on the classpath of your application.
And the How question, you can attach the javadoc jar file of the Twitter jar to the Twitter jar's classpath entry in Eclipse. Right-click your project and select Properties. Go to the Java Build Path entry and find the Twitter jar under the Libraries tab. If you expand the Twitter jar entry you will see 'JavaDoc location'. You can specify the javadoc jar there.
In Eclipse, right-click your project, select Properties. Now find the entry for Java Build Path and select it. Click the "Libraries" tab if it is not already selected. Click the "Add external JARs..." button. Follow the prompts.

Exporting resource folder with jar

I have a folder called "resources" in my Eclipse project. It's used as source folder. Everything works well compiled from Eclipse. I wanted to try it on Ubuntu so I exported it to runnable jar and tried to run but it gave me FileNotFound Exception. I checked the file structure inside the JAR and there's no "resources" folder, only files that should be inside. I'm accessing files with
File file = new File("resources/hotkeys.ini");
I also tried
InputStream in = getClass().getClassLoader().getResourceAsStream("/resources/hotkeys.ini");
but it gave me null.
How to force Eclipse to export the folder along with jar?
When you use "Export" functionality, you need to select all resources that need to be exported (it is visible at the top of the Eclipse export wizard window).
You can find the answer here as well: Java: export to an .jar file in eclipse
You can open the jar with 7zip or Winzip, and check the path.
Try
getClass().getResourceAsStream("/resources/hotkeys.ini");
(Or try removing the first / when with getClassLoader().)
Especially check the case-sensitivity of the paths. As opposed to Windows, inside a Jar it is case-sensitive.

Exporting android project to jar (that includes other jars)

I've written a Android Game Engine library and would like to export it to a jar file. I've tried to do this with the following:
export > Jar File > Finish
As soon as I add the jar to the build path of another Android project, I get a little box in my code saying:
The type foo cannot be resolved. It is indirectly
referenced from required .class files
I believe this is because my Game Engine library includes other libraries in forms of jars.
How can I add those jar files to my Game Engine jar file?
You can decompile the other jar file using JDui, take what you need, and add it to your library. It all depends on how big is what you need..
Also, check whether you are using proguard in the export. You would have to be careful on how to configure it.
I assuming you are using Eclipse. You also need to use Properties->Java Build Path->Libraries->Add Jar button to add your jar file to the target project

Categories

Resources