Creating an executable installer in Java - java

I am interested in making a one-click installer for my C# application.
I had the the framework of the application down. The logic of the application in the installer() method was:
public static void installer(){
deleteLegacyFiles(); // deletes old files through a find method
moveSQLite(); // moves the database file
if(checkRevit2013()){ // checks whether Revit '13 is installed
movePlugin2013(); // moves my plugin into the Addin folder or Revit
}else if(checkRevit2014()){ // check whether Revit '14 is installed
movePlugin2014(); // moves my plugin into the Addin folder or Revit
}else{
System.out.println("It does not look like you have either Revit 2013 or Revit 2014 installed.");
}
}
However, this Java script (not Javascript, but a Java script) really only took three folders from the /Desktop/ and copies them to their respective target folders. I am interested in a solution that turns all my three folders into one executable file (something like an .exe or .msi) and do the above actions.
Are there any solutions for this for Java? Something that packages multiple folders/files together and then allows for one-click solutions for installation? I'm not exactly how to phrase what I want, as this is my first software development project. Any suggestions are welcome.

You can create a single executable jar file in java. This jar would have an application that does all the copying you've listed above. But instead of copying from the desktop, it would copy directories that are included in the executable jar. A jar is a zipped file type (in fact you can change the extension from jar to zip and examine the contents).
Your strategy will be to create a regular java application, package as an executable jar. Include the directories you want to install as resources in the jar. Check out the jar documentation for all java utility methods and classes to manipulate jars.
http://docs.oracle.com/javase/tutorial/deployment/jar/

Are you looking for making/building an executable jar file? If so you can use something like one-jar.
http://one-jar.sourceforge.net/index.php?page=introduction&file=intro
Here are the steps:
Create an executable JAR file with your application's CLASS files in it. (Navigate to bin directory of workspace) Name this "main.jar"
jar cfm main.jar manifest.txt *.class OR [jar cfm main.jar manifest.txt .]
Create three directories: MAIN, LIB, and BOOT
Place your "main.jar" file in the MAIN directory.
Place the jar files that your main application depends on in the LIB directory.
Naigate to Packaging- Create a new JAR file out of the MAIN and LIB directories. Name this one "MyUtil.jar". You do not need to add a manifest or do anything special to this file. It does not need to be executable. Just make it so that it contains the contents of the MAIN and LIB directories.
jar cf MyUtil.jar main lib
Extract the contents of the "one-jar-boot.jar" file into the BOOT directory.
Navigate to the BOOT directory, and update the "MyUtil.jar" file with the following:
jar -uvfm ../MyUtil.jar boot-manifest.mf .
Your "MyUtil.jar" file should now be executable. Test it.

Related

How to Import an External .class File in an Eclipse Java Project

I am trying to include a .class file through an import in a .java class I am creating. I need the .java class to look the same as what I need to deploy on the application server. I have created a directory structure to put the Context.class file in.
I updated the build path by using "Add External Class Folder..." and selected the "oracle" directory containing apps>fnd>common.
However, Eclipse still can't find the Context.class file referenced in the import.
What am I doing wrong?
You need to select the classes (C:\Users\badams\java\classes) directory instead of the oracle directory when using Add External Class Folder....
You should provide the root of the directory that represent your java package structure.
As an alternative, you can create a jar archive with the contents of the C:\Users\badams\java\classes directory and add that archive as a jar dependency as well.
You can use for this purpose the builtin SDK jar utility. Please, change you current directory to C:\Users\badams\java\classes and run from a command prompt or PowerShell terminal something like:
jar cvf oracle-fnd.jar *
Then add the generated archive to your project as usual.

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 turn folder into jar - Code

Hi im busy on a application that decompiles a jar the pastes files into the folder of the decompiled jar, it then compresses the folder into a jar.
Decompiling and copying works, but i can't manage to get the folders contents to be jared (compressed into jar), i did about 3hrs research and found only outdated methods. please help.
-Regards
marko5049
EDIT MORE INFO:
I apologize i mean i cant get my application to turn a folder into a jar file, my application is an modification installer for a jar file. and it extracts the jars files, then adds the modification and then, is supposed to then turn the folder back into a jar file so that the modification is installed. The jar file is not executable.
This worked for me for a MAC OSX:
Open Terminal at the folder with the jar file and run the following commands
unzip mylib.jar -d jarfolder
//You can then change whatever you need and finally run the command below
jar cvf mylib.jar -C jarfolder/ .
Given that you want to create the JAR through code; you can use JarOutputStream for that. There is an example at this link that contains code to create a JAR file given a File[] containing all the input files.
As for creating the list of files given a starting input path, see Recursively list files in Java.
You could either build a list of files then just use code like in the above example, or you could recursively scan files and add them to the JAR as you go.
If you are using Java 7 and you know your users are too you can also use Files.walkFileTree() with a FileVisitor that adds entries to the JAR as it visits files.
Original answer before OP clarified:
Is there something wrong with:
jar cf my-application.jar folder1 folder2 folder3 etc
The JDK comes with a jar utility to create JAR files.
You can read an official tutorial on it here: Creating JAR Files. It is very straightforward.
If you want to create a runnable JAR, you can create a manifest file that has the main class and other options in it. The linked tutorial describes that process.
The short answer is, ZIP the folder, then rename it to a JAR file.
for windows just make the folder as winrar file.,
to do this right click the folder and click "7 -zip" then
choose "add to foldername.zip".
now a rar file is created with the same folder name.
Then open the cmd in current folder directory
type "mv foldername.zip foldername.jar"
Now you got the executable jar file with your corresponding folder.
The easiest way to make it .. put your folder to C:\Program Files\Java\jdk1.8.0_221\bin and then reach till the same path from CMD then run this
jar cvf Name_your_jar.jar folder
Following command worked for me in Windows 10 and jdk-8u212
jar cf my-application.jar folder1 folder2 folder3 etc
You can put your files in a zip folder. Then convert the zip file into Jar format.A .jar extension file is a Java Archive format file. It is used to store a large amount of files into one single file. You can try a free online file converter without downloading a new software on your computer. There are various online file converters available on Google. I would recommend Convert zip to jar
I hope it helps.
I just found this question and its answers are more useful for your problem:
how to zip a folder itself using java
2 tips :
1、a jar is exactly a zip. So, you just need to zip your folder, and rename it to jar
2、be careful that you should zip your whole folder without changing the relative path of the files, but not just extract all the .class files and zip them together. Because when you run the jar, the class package should be consistent with its path.
I suggest trying to create a regular .ZIP file in Windows.
You need to get 7-zip in order to view the .JAR file you are creating. You should just paste contents into the .ZIP, then rename the file type from .ZIP to .JAR, this worked for me and I hope this works for you.
.JARs are basically .ZIPs created by the Oracle Java client, so you need special file viewing software such as 7-zip or WinRAR to view it for some reason.
You can also revert .JARs to .ZIPs by renaming the file type. You might have to mod your computer with RegeEdit or something to have access to renaming your file types.
I hope this helps.

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.

Updating .class file in jar

I want to update a .class file in a jar with a new one. What is the easiest way to do it, especially in the Eclipse IDE?
This tutorial details how to update a jar file
jar -uf jar-file <optional_folder_structure>/input-file(s)
where 'u' means update.
Do you want to do it automatically or manually? If manually, a JAR file is really just a ZIP file, so you should be able to open it with any ZIP reader. (You may need to change the extension first.) If you want to update the JAR file automatically via Eclipse, you may want to look into Ant support in Eclipse and look at the zip task.
Use jar -xvf to extract the files to a directory.
Make your changes and replace the classes.
Use jar -cvf to create a new jar file.
Simply drag and drop your new class file to the JAR using 7-Zip or Winzip. You can even modify a JAR file that is included in a WAR file using the parent folder icon, and click Ok when 7zip detects that the inside file has been modified
Jar is an archive, you can replace a file in it by yourself in your favourite file manager (Total Commander for example).
A JAR file is just a .zip in disguise. The zipped folder contains .class files.
If you're on macOS:
Rename the file to possess the '.zip' extension. e.g. myJar.jar -> myJar.zip.
Decompress the '.zip' (double click on it). A new folder called 'myJar' will appear
Find and replace the .class file with your new .class file.
Select all the contents of the folder 'myJar' and choose 'Compress x items'. DO NOT ZIP THE FOLDER ITSELF, ONLY ITS CONTENTS
Miscellaneous - Compiling a single .class file, with reference to a original jar, on macOS
Make a file myClass.java, containing your code.
Open terminal from Spotlight.
javac -classpath originalJar.jar myClass.java This will create your compiled class called myClass.class.
From here, follow the steps above. You can also use Eclipse to compile it, simply reference the original jar by right clicking on the project, 'Build Path' -> 'Add External Archives'. From here you should be able to compile it as a jar, and use the zip technique above to retrieve the class from the jar.
Editing properties/my_app.properties file inside jar:
"zip -u /var/opt/my-jar-with-dependencies.jar properties/my_app.properties". Basically "zip -u <source> <dest>", where dest is relative to the jar extract folder.
High-level steps:
Setup the environment
Use JD-GUI to peek into the JAR file
Unpack the JAR file
Modify the .class file with a Java Bytecode Editor
Update the modified classes into existing JAR file
Verify it with JD-GUI
Refer below link for detailed steps and methods to do it,
https://www.talksinfo.com/how-to-edit-class-file-from-a-jar/
1) you can extract the file into a folder called
jarname.jar
and then replace the file in the folder, handy if you are updating the class a lot while debugging
2) you can extract the jar replace the file then the jar it up again
3) Open the jar with 7 zip and drag and drop your new class in to copy over the old one
You can find source code of any .jar file online, import the same project in your IDE with basic setups. Make necessary changes in .java file and compile it for .class files.
Once compilation is done You need to extract the jar file, replace the old .class file with new one.
And use below command for reconstruct .jar file
Jar cf test.jar *
Note : I have done so many time this changes in our project, hope you will find it useful.
An alternative is not to replace the .class file in the jar file. Instead put it into a new jar file and ensure that it appears earlier on your classpath than the original jar file.
Not sure I would recommend this for production software but for development it is quick and easy.

Categories