Why does my JAR file not create a serialization? - jar

I created a jar file from a Java project. I can even run it now, but it does not save the serialization file that is stored correctly when I run the program from normal binary code outside of the JAR file.
Can’t you create new files when you have a jar file?
This is how I write the file:
FileOutputStream fileOut = new FileOutputStream("data/vocabulary.ser");
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(this);
out.close();
fileOut.close();
My JAR file looks like this:
META-INF
MANIFEST.MF
name
stefankoch
…
where name/stefankoch/… is my project namespace.
I tried creating a data directory in the jar-file (root), but it did not work either. There also is a data directory within the directory the jar-file resides in, but that one is not taken either.
How can I allow jar files to create files?

basically, you should not add information entered by the user into a JAR file, they belong to the user’s home directory.
This can be read with
System.getProperty("user.home")
And files within the user home directory can also be used from within a JAR file.

Related

Unable to read xml file when using exe4j to package jar into exe

Recently I encountered a problem when using exe4j to package the jar into exe. My xml configuration file is placed in the same directory of the exe, but after the package is completed, the exe will look for the xml file from the temporary folder.
These are the instructions in the exe4j help documentation
For some applications (especially GUI applications) you might want to change >the working directory to a specific directory relative to the executable, for >example to read config files that are in a fixed location. To do so, please >select the Change working directory to: checkbox and enter a directory relative >to the executable in the adjacent text field. To change the current directory >to the same directory where the executable is located, please enter a single >dot.
The error when i running the exe :
[ERROR] In Log's init,cann't read config file, file=/C:/Users/**/AppData/Local/Temp/e4jEA8.tmp_dir1543543191//sys_log.xml
Why is the program not looking for this xml from the exe's sibling directory, and how can I find it from this directory?
If you are using the "JAR in EXE" mode, use
System.getPrpoerty("install4j.exeDir")
to get the parent directory of the executable.

How to update a JAR file with a modfied Java file

I have one JAR named as abc.jar with 15 class files. In JAR file I need to modify xyz.class, so I decompiled to xyz.java and modified it.
How can I update that JAR with the modified Java file? While trying to create (compile) the JAR file I am getting errors.
You can add your newly compiled class to the jar and overwrite the old one, here is how to add xyz.class and xyz.java to the jar :
jar uf abc.jar xyz.class xyz.java
See here for more about updating a jar file.
Or simply you can use any archiving tool, like winrar to open and add files to the jar.
Compile the xyz.java and replace the xyz.class file directly in jar without building the jar again, if the single class has been modified.

Read file from a directory outside of Web project

my web application project (JBoss AS) needs to read a file which resides outside of this project. For example, the file to be read is under /tmp/.
It looks like classes in the project can't access files under /tmp/. However, it can output to a file in /tmp/.
Is this normal? How to access /tmp/ directory in a Web application?
Use java.io.tmpdir system property.
File tmpDir = new File(System.getProperty("java.io.tmpdir"));
//Use tmpDir to access files in tmp directory
Make sure there are unix permissions to read/write contents on tmp directory.

File in executable jar cannot be found when running on AWS EC2

I have a .jar file executing on a aws ec2 instance which contains the following code:
List<String> lines = FileUtils.readLines(new File("googlebooks-eng-all-1gram-20120701-k"));
the file exists in projectname/res and also in /projectname directly. I included /res in the build path. Also I see that the file exists inside the jar file at the root if I export the .java file in eclipse.
If I run the jar localy on my pc it works fine. But if I run it on a ec2 instance it says:
java.io.FileNotFoundException: File 'googlebooks-eng-all-1gram-20120701-k' does not exist
How can that be?
On your PC it is reading from the actual file on the filesystem - that is what new File means - a file on the filesystem.
To access a resource in a jar file you need to call getResourceAsStream or something similar instead.

File missing when packaging to jar file

I have a problem regarding exporting my Java project into a jar file.
I have this file (application.properties) which contains some database information and located under the project root directory.
There is no problem when running this project on Eclipse. But after exporting into a jar(Runnable Jar file) file, application.properties will not be included in the packaging and that cause the error.
Any suggestion how to fix this problem?
Do you want to have the application.properties reside outside the jar (for writing), or is it read-only? When outside you can have an initial template file in the jar, and copy it to some location.
//String applicationWorkingDir = System.getProperty("user.dir"); // The current constellation.
String userHomeDir = System.getProperty("user.home");
File myApplicationDataDir = new File(userHomeDir + "/.MyApplication");
myApplicationDataDir.mkdir();
File propertiesFile = new File(myApplicationDataDir, "application.properties");
Also using the Preferences API instead of a properties would be a solution.

Resources