Accessing a saved file in a .exe. Java - java

I have written a program in java and have stored some data in a txt file. I then made the project into an .exe file using launch4j, and I was wondering how I can access the data stored in the file which should be in the .exe.
I have tried using getClass().getResource(file).path(); to get the file path, but this doesn't seem to work.

By the time of deployment, those resources will likely become an embedded-resource. That being the case, the resource must be accessed by URL instead of File. See the info page for the tag, for a way to form an URL.

If you used wrapping option of launch4j, then you cannot simply obtain resources from .exe. You would have to somehow parse/reverse engineer the exe file to obtain just the .jar content.
I doubt that there is some software for this, since you can simply decide not to wrap the jar with exe in the first place. Then you will have separate .exe file that server as a launcher for a independent .jar file. Its quite easy solution and really simple in an application.

Related

Is it possible to write to a settings XML stored within a JAR at run time?

I have a large number of user settings that I want to store within an XML file. If it is possible, I would like to keep that XML file within the JAR itself, but so far I have found nothing that permits me to do that (lots of searching on Google turned up nothing).
I plan to use JAXB to handle reading and writing the XML file but first I need to know if it's possible to write to the XML file within the JAR during run time.
So far getClass().getResourceAsStream() seems to be on target except it only returns an input stream to read from the file. Is it possible at all to write to a file stored as a resource within a JAR file at run time? To open that file for Output?
The Java process will lock dependencies on the classpath. You won't be able to change your JAR because the OS will tell you that it is in use.
java -cp "your_jar.jar;your_jar2.jar" YourMainClass
Only way I can think of is to load your JAR at runtime, so you can change a second jar that would be part of your application.
I (kinda) understand why you are doing it, but don't do it. Keep configuration out of your JAR.

How to open a .jar file within Java

I've been trying to make a program that, evidently, opens a .zip file and extracts its contents into a .jar file(minecraft.jar, for modding Minecraft), but couldn't find any way to open a .jar file within Java to do so. Is opening a .jar file(not running it) and adding/deleting files within the .jar file possible?
First, #McDowell comment is the root of your solution: the java.util.jar package is where you'll find the tools you need.
Second, modifying the content of a JAR file on the fly is possible, but complicated and rather a lot of work. Would it not be sufficient for your purposes to open both files (the ZIP and JAR ones, if I understand correctly), extract them in a temporary directory, then re-compress the whole lot?
It seems to me the result would be exactly what you're looking for. It might take a bit of a while to run, but since it's a one-off, I don't feel it's a major issue.

Java Game refering to files

I have started getting into game programming.
My question is, that when I am working with files, either parsing data, writing to files, etc. Should I be using relative path names, or absolute pathnames, or something else which is better. I've heard about using jar files, but I am not sure
1. how that works
2. if it is a good way to do it.
So when developing a game that will be cross platform, what is the best method for managing files that the program will need to read from and write to.
there are several ways in which you can ship your code as a product. the most common are
packaging everything in one executable jar file.
having a set of folders where you place all necessary resources.
minecraft, for example, is written in java and distributed as a single executable jar file that contains all necessary class files and resources. to run the game (assuming you have java installed) all you need to do is double-click the jar file.
read this short tutorial about how to add a main class to a jar file.
either way, always treat classes and resources in your code as if they're in your classpath. for example, if you have a my.properties file on the root of the source tree then load it by using 'my.properties'. if you put it under a 'conf' folder then use 'conf/my.properties'.
i think it is the safest way not to get lost.
are you using maven?
The jar file is a zip of all your compiled *.class files and your resources. You can safely load your resources and even default data FROM a jar if you package your program, but you can NOT safely write data back to the jar. This detail is answered in depth already at
How can an app use files inside the JAR for read and write?
For information on how to package a jar see
http://docs.oracle.com/javase/tutorial/deployment/jar/

Serialized files don't work when project is converted to executable jar?

I made my java project into an executable jar using the export to jar option in eclipse. The jar runs as expected, except that it does not use any of the serialized files. I can see that clearly from the GUI. What could be the reason for this problem and how do I fix it ?
I saw this related question - Why does my JAR file not create a serialization?
But it does not tell me exactly how to get around this problem. It looks like you cannot pack a folder into a jar. Why ? Because code could accidentally/intentionally continue to add data into that folder and make the whole jar occupy the hard disk ?
How do I create some kind of structure in which I pack my executable jar and its serialization folder ?
Answering this question:
How do I create some kind of structure in which I pack my executable jar and its serialization folder ?
A common approach is to have a well-defined place to store serialized files, settings, etc, that does not depend on where the program has been executed from. Usually it is user's home directory, or Application Data in case of windows. I used this code to store my application settings:
String home = System.getenv("APPDATA");
if (StringUtils.isEmpty(home)) {
home = System.getProperty("user.home");
}
CONFIG_HOME = new File(home, ".myProgram").getAbsoluteFile();
CONFIG_HOME.mkdirs();
So on windows it will use AppData and on *nix systems it will use user's home. The dot in front of myProgram is to make it hidden on *nix platforms, which is a common practice.
EDIT For your question in your comment:
on my linux machine there is no APPDATA env variable so this code will create a directory /home/myUser/.myProgram. On windows it will be something like c:/Users/myUser/AppData/Local/.myProgram. On MacOSX, no idea.
You need your JAR to use the same path for reading the Serialized Files as your code in eclipse.
So you make a properties file containing the directory with your serialized objects.
Then, this is the same for both your JAR and our project.
See also: http://www.mkyong.com/java/java-properties-file-examples/
You can use
AClass.class.getResource(String str);
//or
AClass.class.getResourceAsStream(String str);
AClass: one of your classes.
str: file location which you want to read.
For example;
if your class hierarchy seem like this:
+src
+-com
+-test
|-AClass.java
+-util
+-PrintUtil.java
+-resources
|-Bouble.png
|-Mouse.png
+-Ocean.png
and for reading "Mouse.png" image, you can this with a lots of ways:
AClass.class.getResource("/resources/Mouse.png");
//or
PrintUtil.class.getResource("../resources/Mouse.png");
...
You can't write inside a jar file while you are using/running the jar file. When you put the jar file in you classpath or you run the program from jar directly, the jar will be locked by your jvm, hence it won't allow you to update the same jar file which you are currently using.
The solution given by people which says use resource as stream will work if your classes are there in a folder, not in an archive (which you are using).
As an archive you can't directly update it, you need to do following steps (by yourself or by 3rd party api),
Extract in temp location
update the files
re archive
Now as the jar file is locked, you won't be able to do the third operation, which is not even safe. As an example when you are running a jar file, try to rename it, it won't happen, if it happens, the jar file is not yet locked by the jvm, it gets locked whenever you call a class which is inside the jar file.
For better and secure serialization and file saving please look into this: java.util.prefs.Preferences.

Modifying a file inside a jar

I would like to modify a file inside my jar. Is it possible to do this without extracting and re jarring, from within my application?
File i want to modify are configuration files, mostly xml based.
The reason i am interested in not un jarring is that the application is wrapped with launch4j if i unjar it i can't create the .exe file again.
You can use the u option for jar
From the Java Tutorials:
jar uf jar-file input-file(s)
"Any files already in the archive having the same pathname as a file being added will be overwritten."
See Updating a JAR File.
Much better than making the whole jar all over again. Invoking this from within your program sounds possible too. Try Running Command Line in Java
You can use Vim:
vim my.jar
Vim is able to edit compressed text files, given you have unzip in your environment.
Java jar files are the same format as zip files - so if you have a zip file utility that would let you modify an archive, you have your foot in the door. Second problem is, if you want to recompile a class or something, you probably will just have to re-build the jar; but a text file or something (xml, for instance) should be easily enough modified.
As many have said, you can't change a file in a JAR without recanning the JAR. It's even worse with Launch4J, you have to rebuild the EXE once you change the JAR. So don't go this route.
It's generally bad idea to put configuration files in the JAR. Here is my suggestion. Search for your configuration file in some pre-determined locations (like home directory, \Program Files\ etc). If you find a configuration file, use it. Otherwise, use the one in the JAR as fallback. If you do this, you just need to write the configuration file in the pre-determined location and the program will pick it up.
Another benefit of this approach is that the modified configuration file doesn't get overwritten if you upgrade your software.
Not sure if this help, but you can edit without extracting:
Open the jar file from vi editor
Select the file you want to edit from the list
Press enter to open the file do the changers and save it
pretty simple
Check the blog post for more details
http://vinurip.blogspot.com/2015/04/how-to-edit-contents-of-jar-file-on-mac.html
I have similar issue where I need to modify/update a xml file inside a jar file.
The jar file is created by a Spring-boot application and the location of the file is BOOT-INF/classes/properties
I was referring this document and trying to replace/update the file with this command:
jar uf myapp.jar BOOT-INF/classes/properties/test.xml
But with this, it wont change the file at the given location. I tried all the options also but wont work.
Note: The command I am executing from the location where jar file is present.
The solution I found is:
From the current location of jar file, I created folders BOOT-INF/classes/properties
Copy the test.xml file into the location BOOT-INF/classes/properties.
Run the same command again. jar uf myapp.jar BOOT-INF/classes/properties/test.xml
The xml file has been changed in the jar file.
Basically you need to create a folder structure like where the file is located into the jar file. Copy the file at that location and then execute the command.
The problem with the documentation is that, it does not have enough examples as well as explanation around common scenarios.
This may be more work than you're looking to deal with in the short term, but I suspect in the long term it would be very beneficial for you to look into using Ant (or Maven, or even Bazel) instead of building jar's manually. That way you can just click on the ant file (if you use Eclipse) and rebuild the jar.
Alternatively, you may want to actually not have these config files in the jar at all - if you're expecting to need to replace these files regularly, or if it's supposed to be distributed to multiple parties, the config file should not be part of the jar at all.
To expand on what dfa said, the reason is because the jar file is set up like a zip file. If you want to modify the file, you must read out all of the entries, modify the one you want to change, and then write the entries back into the jar file. I have had to do this before, and that was the only way I could find to do it.
EDIT
Note that this is using the internal to Java jar file editors, which are file streams. I am sure there is a way to do it, you could read the entire jar into memory, modify everything, then write back out to a file stream. That is what I believe utilities like 7-Zip and others are doing, as I believe the ToC of a zip header has to be defined at write time. However, I could be wrong.
Yes you can, using SQLite you can read from or write to a database from within the jar file, so that you won't have to extract and then re jar it, follow my post http://shoaibinamdar.in/blog/?p=313
using the syntax "jdbc:sqlite::resource:" you would be able to read and write to a database from within the jar file
Check out TrueZip.
It does exactly what you want (to edit files inline inside a jar file), through a virtual file system API. It also supports nested archives (jar inside a jar) as well.
Extract jar file for ex. with winrar and use CAVAJ:
Cavaj Java Decompiler is a graphical freeware utility that reconstructs Java source code from CLASS files.
here is video tutorial if you need:
https://www.youtube.com/watch?v=ByLUeem7680
The simplest way I've found to do this in Windows is with WinRAR:
Right-click on the file and choose "Open with WinRAR" from the context menu.
Navigate to the file to be edited and double-click on it to open it in the default editor.
After making the changes, save and exit the editor.
A dialogue will then appear asking if you wish to update the file in the archive - choose "Yes" and the JAR will be updated.
most of the answers above saying you can't do it for class file.
Even if you want to update class file you can do that also.
All you need to do is that drag and drop the class file from your workspace in the jar.
In case you want to verify your changes in class file , you can do it using a decompiler like jd-gui.
As long as this file isn't .class, i.e. resource file or manifest file - you can.

Categories