Writing java makefile for copy xml files to output directory - java

In my Java application,I want to add a directory inside my package structure. This directory contains some XML files. Since these files cannot be compiled, I want to copy these files during war make process. How to write a makefile for this?

Without knowing precisely what your directory structure looks like, I would simply say that you handle this in the same fashion as putting your class files into a .war file. A .war file is little more than a .zip file and doesn't really care what you put into it.
Going forwards, you should dump the makefile and investigate something more Java-centric. I would suggest Maven for its convention-directed approach. For example, the war goal of Maven will simply assume a directory structure and successfully build a .war file of suitable contents within those directories.

Related

Reading and writing files within Jar

I have finally completed a program in Java and I have to upload it.
The problem is that I have to upload also the executable .jar file and not only the eclipse project.
The main functionality of my program consists by reading and writing .xml files (for example one file is used to read and add new users), and the files in the project folder are so located:
-Project Name
src
default package
main and all other classes
file1.xml
file2.xml
So the two .xml files are in the root of the project.
My question is: It is better to save the .xml files in the JAR and then writing and reading them from the executable program or it is better to store them in a folder outside the .JAR and reading and writing them as externally files?
It is a good practice to create a folder like that?:
-ProjectName
file1.xml
file2.xml
project.jar
I read in Stackoverflow a lot of people having my same issue and a lot of people doesnt know how to manage this problem properly.
Thank you in advance for the reply :)
Changing files in JAR-files can have all sorts of problems. That starts with simple things such as what should happen when you want to update your program to the newest version? Usually you'd just swap the jar, but then you loose everything you edited so far. You'd need a process to update inside the jar.
Other problems include that for changing the jar file you need to open it, possibly realign contents and rewrite the index which could conflict with the JVM that is reading the jar at the same time causing odd behaviour. On some systems (windows...) the Jar file might even be locked while the application is running and thus you cannot write to it at all.
I'd suggest that you add "default files" (in case that your files are initially not just empty) to you Jar file that represent the initial state. If the application is started you check if the XML files exist in the some normal writable directory and if they don't just copy the default files to that directory. This allows you to deploy still just a single jar file, but once started the appropriate files will be created.
You may read a XML file located inside the executable Jar but it is not possible to update (write) a XML file located inside that executable Jar file. So the best option would be:-
-ProjectName
file1.xml
file2.xml
project.jar
The jar should be kept read-only, the XML "files" inside the jar should be read using getResource[AsStream] (class path). You can use those resources as templates to create a copy in the user's (or application's) directory/sub-directory. For the user's directory:
System.getProperty("user.home")

Java Jar: Specifying independent filepaths

So, at work, we have a java application that we license to clients. Frequently, our clients require very specific things that we don't want in the core product, so we make those changes, package them in a jar, and place the jar in the libs directory of the branch in our VCS that we use for that client.
We want to start packaging the .java files in the jar along with the .class files, just as an extra backup for our source code. The thing is, in our main codebase, all our .class files are in a filetree starting at a folder called "out", and all our .java files are in a filetree starting at a folder called "development".
So, I don't want the folder that's unique to both of them to be where I run the jar command from the command line. The reason for this is the jar utility builds a folder structure that mirrors what it sees when it builds the jar, and I need my folder structure to start at com.companyname.etc... for the class file.
Is there any way to specify totally divergent filepaths when creating a jar? Or to specify the folder structure that should be the destination when the files are dropped in the jar?
Thanks in advance.

How to package a jar file along with reference files?

Using eclipse, exporting a runnable jar file is pretty simple when I'm only using the application on my computer. Any files that the program is using (sprite sheets, audio tracks, etc.) only exist on my computer, so sending solely the jar to another machine won't work.
What is the easiest way to package a jar along with all the necessary files so that I could run the program on any machine?
I see from your tags that you are working in Eclipse. I am not sure if this method will work in other IDEs and I don't think it'll work at all if you're doing everything manually (it relies on the compiler automatically copying resources over to the bin folder.
The simplest way (at least what I use) is to define another sourcefolder (I like res).
Then you can just add packages to this source folder and dump the relevant images. Then rebuild your project.
Finally, you can use getClass().getResourceAsStream("package/path/file_name.whatever"); to get your files.
After an export as jar, it should work, even on other machines.
If you don't require the other files to be actual files on the file system (which means you can't use File, FileInputStream, etc) then you can use the resource system. If you put them inside the JAR, you can access them like this:
InputStream fileStream = SomeClassInYourJarFile.class
.getResourceAsStream("/path/to/file.png");
This example would give you an InputStream reading from the /path/to/file.png entry in your JAR file - that is, the "file.png" file inside a folder "to" inside a folder "path".
This does not require the files to be in a JAR file - it can load them from wherever your .class files are stored, JAR or not. If you put them in your source folder, Eclipse will automatically copy them to that place - so the above line would also work if you had a package path.to containing a file called file.png.

Netbeans Clean & build classes

When I clean and build a project in NetBeans, the .jar file appears in the dist folder, like it's supposed to. But what if I have multiple files under the project? What happens to those files? E.g. I have a Game project, and under it are the different characters(knight, rogue, etc.) but I only see a game.jar file when I clean and build, I want to know what happens to the individual files. Thanks
Those files should be in the jar file as compiled .class files. It's easy to double check what's in the jar file since it's in zip format. You can use a program like 7-Zip to open it, or rename it to the zip extension (e.g. from mygame.jar to mygame.zip) and whatever OS you're using probably has some way to open it.
When you open or extract the jar file you'll find the compiled class files in a directory structure that reflects your package structure. For example, if you have Knight.java in the directory src/game/characters/Knight.java in the jar file you'll find something like classes/game/characters/Knight.class.
The name "jar" is an abbreviation of "Java archive". It stores all the classes and other resources (for example, images) in a project.
The classes you have defined in .java files will be compiled into .class files - these are contained in the .jar file.
All resources get compiled into the JAR file. If you want a separate JAR for the resources, you'll need to split the project into two maven projects: one jar for the code, one for the resources. You can then create a third project that would generate a distribution.
That's a lot of work, though. It's.a lot easier tO keep everything in one JAR unless you have explicit dynamic loading requirements.

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