Java + Eclipse SWT project, trying to get folder path into the "jar" - java

I'm developing a project in Eclipse with SWT, where the interface is shown in a browser and for that, I use the XULRunner (Firefox) instead of using the standard browser.
I thought to compile the jar with the xurlrunner folder inside, so I created a source folder called "plugins" and copied the xurlrunner folder inside and got the path:
URL xulrunner = getClass().getClassLoader().getResource("xulrunner");
File file = new File(xulrunner.toURI());
System.setProperty("org.eclipse.swt.browser.DefaultType", "mozilla");
System.setProperty("org.eclipse.swt.browser.XULRunnerPath",
-> file.getAbsolutePath());
It worked, but only when run from within the Eclipse before exporting, however, the big problem is that when I export for a jar executable, the JVM can not load, of course, the reason is that file.getAbsolutePath() could not point to into the jar.
For images, I use getResourceAsStream() and it works very well once compiled, but when it is to transform a folder path inside the jar to String is that it is difficult.
QUESTION:
Is there any other way I can point to a folder into the jar that can be turned to String?
PROPOSAL
if there is no a way to keep the xurlrunner folder into the jar, I'll create an installer apart to extract it from a zip to the same location of the program installation and dynamically point to it.

SWT expects the XULRunnerPath to point to a regular filesystem folder. Thefore the entire xulrunner folder must be extracted into a regular folder before setting the system property.

Related

Java get path of executing jar without .jar file

I am trying to export my app into jar using intelij Idea artifacts. I export everything except for resources (14,5GB is in my opinion too much for .jar file) so I have put /res folder inside same folder as jar.
Right now my structure looks like this:
/SAR.jar
/res/colors/
/res/icons/
/res/map/
However I cant access this resources. I am using the following code to get path:
dir = new File(getClass().getProtectionDomain().getCodeSource().getLocation().toURI().getPath() + "/res/icons/");
It works when running from IDE and it prints correct location. But when I build an artifact and run it through cmd I get the null pointer and it prints this path: D:/JavaProject/SAR/out/artifacts/SAR_jar/SAR.jar/res/icons. But my resources are not inside jar file they are only in same directory.
Any ideas? I have done only android projects in path and I simply cant get this resources system with jars to work.
Thanks in forward
You are trying to access the ressources with a path dependent of your source code location, which is obviously not the case since you separated the code (or bytecode in the case of the jar) and the ressources. You must access them with a path relative to the jar or an absolute path.
I think you just need the following path (if the jar was ran from the directory containing the jar) :
new File("res/icons/icon.png")

Location of image in exported project folder

In my current code I write the specific location of the images I want to use in my project, now, these locations are only correct until I move the image to a different directory or open the application in a different computer.
Where do I place the images (where to place them in the exported folder) I want to use in my project?
The exported project is a zip file, once extracted, I have 2 folders within the extracted folder, one named nbproject other is src, one text file named build another .mf file called manifest inside nbproject I have two text files and two .properties files inside src I have my four classes.
Where do I put the images I want to use in the project? And once I place them, what directory do I write in my project?
Here's an example of how I use an image:
// Main menu background image
bgg[0] = new ImageIcon("D:/NetBeans/NetBeans projects/Java/Project Images/bg option for Vanguard.jpg").getImage();
And then I draw the image all over the screen
My exported folder contents:
http://i.imgur.com/qd4PeJo.png
http://i.imgur.com/W5YQ7OC.png
EDIT: Thought it worked but it keeps giving an exception on other people's computers, perhaps this isn't the reason beause I moved the images around in my PC and it worked but still.
Since you are writing the directory in your code.
bgg[0] = new ImageIcon("D:/NetBeans/NetBeans projects/Java/Project Images/bg option for Vanguard.jpg").getImage();
When you run the project in another computer or move the image to another location, the program can't find the path to the picture, so there is an error. Instead of using the full path. Copy the images to your project folder and use relative paths.For instance : "images/Vanguard.jpg".
The path where your program looks for resources can depend on how you run the program. If you ran it via console/terminal the initial path starts at the path of the *.class file you ran. If you run it from an IDE this path may change, in Eclipse the path starts at the Project dir and not the src folder. You can find out the exact path by calling System.getProperty("user.dir") that will return the current working path as a String.
Don't use a strict path, use a relative path like so:
Main.java
String path = "Project Images/bg option for Vanguard.jpg";
bgg[0] = new ImageIcon(getClass().getResource(path)).getImage();
In order for this to be loaded, you must place a folder called Project Images in the directory in which your Java files are. Then place the image bg option for Vanguard.jpg in this folder. Be sure to compile the program in you IDE so it can make a copy for the compiled version.

Placing application files in bin folder

How can I place the files being used by my application, more particularly text files that contains data that my Java program uses. For the images since it is static, I just copy and paste them in the bin folder. But I have some text files that I create during runtime and I don't know where to place them. I need a place where I can save them in and edit them sometime.
By the way, I am using eclipse IDE.
And how would I code it? Like retrieving etc.
I am reading files with Scanner, creates them with Formatter
If you using Eclipse IDE you can just place the text files in the source folder and eclipse will copy them to the bin folder when building the application. When editing the files in the source folder within Eclipse it will update the copy in the bin folder. When you edited them with an external program you need too choose the “Refresh” menu item in the Eclipse IDE.
Place them relative to the .java file of the class using them so that the copy in the bin folder will be relative to the .class file as well. The you can access them via MyClass.class.getResourceAsStream("path relative to MyClass.class"); which gives you an input stream. This works even if you package your application as a JAR file.

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.

Add resources into a jar upon building

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)

Categories

Resources