I've tried searching through java documentation. I could not find anything on opening a jar file inside of a java application. To further explain what I`m trying to do I will use an example. MinecraftForge has a folder where all of the mods get loaded from using jar files.
I would like to be able to put jar files inside a folder that will be used by the application. Is this possible and could somebody point me in the right direction.
Related
I wrote a little Java app for analyzing .csv files. Now I want to keep reading from and writing to a .txt file, which acts similar to a mini-database. For this purpose I simply added the .txt in my project and used the Files.readString(Path) and Files.write(Path path, byte[] bytes) methods.
When I do this in IntelliJ I have no problems but as soon as I build/export the file with Maven and started with the created launcher the app didn't work because the project structure / file organization isn't the same anymore.
I also tried to just add the .txt file to the exported folder afterwards but even then I couldn't manage to implement a relative path to the file.
I'm still relatively new to programming and it's just a small app so I don't think mySQL would fit my needs. I've also read that I could store the data in a property file but I don't know if that would be the right way to archive what I want. Certainly it must be possible to somehow keep a .txt for reading and writing in your exported project. Does someone have an idea?
If you use a ยด*.txt` file for storing, that file cannot be inside the jar because the jar cannot change its contents while running.
You need to put the file somewhere else, either at some dedicated location (let the user choose/configure one), or next to the jar. To figure out your own execution path, you can use
How to get the path of a running JAR file?
Maven is one tricky tool. You need to go to the pom file and add the resource.
Unable to add resources to final jar for maven project in Intellij.
-I hope this helps
Trader
So I've created just a simple application which I'm using to apply for a highschool internship. It was built using Eclipse.
I initially exported into a runnable .jar file, but the location I initially saved files, ("src/fileDirectories") didn't work on export.I then set the location to "./fileDirectories") which works and the app can actually run, however the .jar file creates the folder directory in the same folder as the .jar file.
This isn't too bad, as I can create a folder with the .jar and the saved files in it, which is fine, but similar to images, I'm wondering if there is a way to save .txt files in a directory to the .jar file so everything works with just the .jar application.
Assuming the files you want to access are static, and are not changed by the application, you can do this using the classpath.
When you access a file using new File("path"), Java will look for it in the file system, relative to the working directory (i.e. where the application was launched from.
An alternative way to access files is to look them up from the classpath - this is the list of locations Java searches for resources. It includes, among other things, the top level of your JAR archive. You can access this as follows:
this.getClass().getResourceAsStream("/my_file.txt")
Build tools will generally have preconfigured directories (e.g. src/main/resources) which get copied from your source tree into the JAR so they can be accessed in this way.
This is great if you have some static resources or config which you want to access at runtime, but don't want to have to pass around with your application.
However, if what you want is a working folder for files which your application will make changes to or create new instances of, like user documents, you don't want this to be editing the JAR - you'll need to make a separate folder for these.
This is a follow up of this question
What I try to do:
I have an eclipse project that uses th Sigar library in order to get the cpu information (among others). In order to do that Sigar needs a library file to work. Each CPU/OS have a different file, all of which are available.I have to pack this into a jar, that other can use my application as a library.
What I did:
added the available library files to my project and can access them like so
System.setProperty("org.hyperic.sigar.path",System.class.getResource("/lib").getPath());
System.load(System.class.getResource(getClass().getResource(
"/lib/libsigar-amd64-linux.so").toString()).getPath());
Both of these seems to work if i run my application as a stand alone application.
When i pack it to a jar and try to run it from an other project, it just doesn't work. I cant make it to see the directory or the file.
This is the structure of my project
I have tried to access the file with any path i could think of, but all returned a NullPointerException.
How can I do this? Using a temp file? I am not sure how i should do this, i would like to load the whole folder and not just one file, since i dont know the hardware/OS of the client application.
Hi guys
I have an Applet in MyApplet.java file. It uses external .jar libraries and I have to get it work on a Web Browser.
1) I am a complete newbie in writing custom classpath, manifest ecc... which is the correct way to compile my .java file to get the .class?
2) Once i have the .class file, what i have to do in my webspace? where to put the jar files? what i have to write in my html page?
A step by step tutorial would be awesome
There are nice resources out there, for example "The Java Tutorial", which has a section on How to write Applets.
But you seem to need even more basic advise on compiling - so take a look at Getting Started, and later at Deployment. Then come back if you have more concrete problems.
I tried to export a Java program from Eclipse to a .jar file, but I came across a problem. It ran fine, but for some reason it didn't find the text files it was supposed to get data from. If anyone could help with this, I would be very grateful.
Where are these text files ? If they're packaged within your .jar file, I would expect to be able to find them within the class hierarchy using ClassLoader.getResourceAsStream(), specifying the complete path relative to the classloader base.
If your program depends on internal text files, put them in classpath and package them in the jar. Find them with the Class.getResourceAsStream.
I guess we need some more information to help you solve this problem. Where are your text files located while debugging and developing your code? Make sure that they are accessible from the path you are running the jar in.