Root Directory Information Included in Compiled Jar File? - java

I have a simple 'Hello World' command line Java application. I build the .jar from the command line - no IDE is used. I find that if the root directory is different (i.e., /hello/ vs. /hello/with/more/dirs/), the final .jar files are not exactly the same. (I compared the two with a Unix diff utility). It appears that the root directory is encoded into the .jar file.
Can anyone verify that is true?
Is there anything else encoded in the .jar file, such as timestamps?
Is there any way to prevent the path from being encoded?

A jar file is just a zip file (plus potentially a manifest). It contains the file list exactly like a zip file does. There is nothing you can do to prevent this.
If you don't want you files inside a directory in the jar, package it from the directory where you files reside. If you're more familiar with zip and don't need a manifest, you can just use that instead too.
There's a good overview of what a jar file actually is in the JAR file article on Wikipedia.

Related

Java FileNotFoundException in jar file

I’m working on a background Spring service that runs from the command line using a nohup command.
I’m hitting the following error:
java.io.FileNotFoundException: class path resource [templates/] cannot be resolved to absolute file path because it does not reside in the file system: jar:file:/Users/gestes/Documents/workspace/bge/bge-proj/myProcess/target/myProcess-0.2.2.jar!/templates/
The jar file is being created and does exist at:
/Users/gestes/Documents/workspace/bge/bge-proj/myProcess/target/myProcess-0.2.2.jar
When I extract the jar file contents, there is a /templates/ directory.
Looking at the exception, there is an “ ! “ after the jar file name, and I thought that tells what it can’t find, but clearly, it is there.
What am I missing?
If in your code you are trying to access the folder using a java File, you cannot do that. You need to use an inputStream
This is because things inside a Jar are not actually files on the disk. They are compiled inside of a jar. Yes it may be there when you extract the jar, but it's not actually a normal disk file when the jar is bundled

How can I open a JAR for editing in Netbeans

I made a JAR file. That runs fine. I've been tinkering with the source and I forgot what changes were made. Now that is not working. Can I pull the .java from the Jar somehow?
A JAR only contains .class files, which are compiled from .java source files. Unfortunately you can't edit the .class files and expect anything useful to come out of it.
This might work, though: JD GUI, a JAR decompiler.
extracting jar will give you just .class files. Still you can try java decompilers which can generate the source from .class files
results are not 100% guaranteed though
A jar file is similar to a zip file in that you can extract the contents. From the oracle website to extract from the command line (you can also use a program like 7zip):
The basic command to use for extracting the contents of a JAR file is:
jar xf jar-file [archived-file(s)]
Let's look at the options and arguments in this command:
The x option indicates that you want to extract files from the JAR archive.
The f options indicates that the JAR file from which files are to be extracted is specified on the command line, rather than through stdin.
The jar-file argument is the filename (or path and filename) of the JAR file from which to extract files.
archived-file(s) is an optional argument consisting of a space-separated list of the files to be extracted from the archive. If this argument is not present, the Jar tool will extract all the files in the archive.
NOTE: A jar file can contain any type of files and not just .class files.

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.

Get JAR path to file

So lets say I have JAR file. User creates some text file and wants to save it. I would like to save it in "resources" folder in my JAR so user can read it later.
But how do I get path to this folder?
You usually wouldn't do this. Updating a zip file (which is what a jar file is, basically) is non-trivial. Jar files aren't designed to be updated with user content (e.g. their "documents" directory, or an application-specific directory). You should store your user's files in an appropriate directory. (Heck, in many operating systems the program files are installed read only anyway...)
Not sure if it is a good idea but it is possible from command line (jar command help):
jar cf myfile.jar dir1/file1.ext1 ;# create jar file and store file1
jar uf myfile.jar dir2/file2.ext2 ;# update jar file with file2
What is possible from command line should be possible thru the Jar Java API

What exactly does a jar file contain?

As an intern, I use company code in my projects and they usually send me a jar file to work with. I add it to the build path in Eclipse and usually all is fine and dandy.
However, I got curious to know, what each class contained and when I try to open one of the classes in the jar file, it tells me that I need a source file.
What does this mean? I come from a C/C++ background so is a jar similar to an already compiled .o file and all I can see is the .h stuff? Or is there actual code in the jar file that I'm using that's encrypted so I can't read it?
Thanks for all the answers!
Edit: Thanks, guys, I knew it was a sort of like an archive but I was confused to why when I tried to open the .class files, I got a bunch of random characters. The output was similar when I tried to open a .o file in C so I just wanted to make sure.
Thanks!
A JAR file is actually just a ZIP file. It can contain anything - usually it contains compiled Java code (*.class), but sometimes also Java sourcecode (*.java).
However, Java can be decompiled - in case the developer obfuscated his code you won't get any useful class/function/variable names though.
However, I got curious to what each class contained and when I try to open one of the classes in the jar file, it tells me that I need a source file.
A jar file is basically a zip file containing .class files and potentially other resources (and metadata about the jar itself). It's hard to compare C to Java really, as Java byte code maintains a lot more metadata than most binary formats - but the class file is compiled code instead of source code.
If you either open the jar file with a zip utility or run jar xf foo.jar you can extract the files from it, and have a look at them. Note that you don't need a jar file to run Java code - classloaders can load class data directly from the file system, or from URLs, as well as from jar files.
The best way to understand what the jar file contains is by executing this :
Go to command line and execute jar tvf jarfilename.jar
A jar file is a zip file with some additional files containing metadata. (Despite the .jar extension, it is in zip format, and any utilities that deal with .zip files are also able to deal with .jar files.)
http://docs.oracle.com/javase/8/docs/technotes/guides/jar/index.html
Jar files can contain any kind of files, but they usually contain class files and supporting configuration files (properties), graphics and other data files needed by the application.
Class files contain compiled Java code, which is executable by the Java Virtual Machine.
http://en.wikipedia.org/wiki/Java_class_file
JAR stands for Java ARchive. It's a file format based on the popular ZIP file format and is used for aggregating many files into one. Although JAR can be used as a general archiving tool, the primary motivation for its development was so that Java applets and their requisite components (.class files, images and sounds) can be downloaded to a browser in a single HTTP transaction, rather than opening a new connection for each piece. This greatly improves the speed with which an applet can be loaded onto a web page and begin functioning. The JAR format also supports compression, which reduces the size of the file and improves download time still further. Additionally, individual entries in a JAR file may be digitally signed by the applet author to authenticate their origin.
Jar file contains compiled Java binary classes in the form of *.class which can be converted to readable .java class by decompiling it using some open source decompiler. The jar also has an optional META-INF/MANIFEST.MF which tells us how to use the jar file - specifies other jar files for loading with the jar.
Jar( Java Archive) contains group of .class files.
1.To create Jar File (Zip File)
if one .class (say, Demo.class) then use command jar -cvf NameOfJarFile.jar Demo.class (usually it’s not feasible for only one .class file)
if more than one .class (say, Demo.class , DemoOne.class) then use command jar -cvf NameOfJarFile.jar Demo.class DemoOne.class
if all .class is to be group (say, Demo.class , DemoOne.class etc) then use command jar -cvf NameOfJarFile.jar *.class
2.To extract Jar File (Unzip File)
jar -xvf NameOfJarFile.jar
3.To display table of content
jar -tvf NameOfJarFile.jar
A .jar file is akin to a .exe file.
In essence, they are both executable files.
A jar file is also a archive (JAR = Java ARchive). In a jar file, you will see folders and class files. Each .class file is similar to a .o you might get from C or C++, and is a compiled java archive.
If you wanted to see the code in a jar file, download a java decompiler (located here: http://java.decompiler.free.fr/?q=jdgui) and a .jar extractor (7zip works fine).
JD-GUI is a very handy tool for browsing and decompiling JARs
A .jar file contains compiled code (*.class files) and other data/resources related to that code. It enables you to bundle multiple files into a single archive file. It also contains metadata. Since it is a zip file it is capable of compressing the data that you put into it.
Couple of things i found useful.
http://www.skylit.com/javamethods/faqs/createjar.html
http://docs.oracle.com/javase/tutorial/deployment/jar/basicsindex.html
The book OSGi in practice defines JAR files as, "JARs are archive files based on the ZIP file format,
allowing many files to be aggregated into a single file. Typically the files
contained in the archive are a mixture of compiled Java class files and resource
files such as images and documents. Additionally the specification defines a
standard location within a JAR archive for metadata — the META-INF folder
— and several standard file names and formats within that directly, most
important of which is the MANIFEST.MF file."
Just check if the aopalliance.jar file has .java files instead of .class files. if so, just extract the jar file, import it in eclipse & create a jar though eclipse. It worked for me.
While learning about JAR, I came across this thread, but couldn't get enough information for people like me, who have .NET background, so I'm gonna add few points which can help persons like myself with .NET background.
First we need to define similar concept to JAR in .NET which is Assembly and assembly shares a lot in common with Java JAR files.
So, an assembly is the fundamental unit of code packaging in the .NET environment. Assemblies are self contained and typically contain the intermediate code from compiling classes, metadata about the classes, and any other files needed by the packaged code to perform its task. Since assemblies are the fundamental unit of code packaging, several actions related to interacting with types must be done at the assembly level. For instance, granting of security permissions, code deployment, and versioning are done at the assembly level.
Java JAR files perform a similar task in Java with most differences being in the implementation. Assemblies are usually stored as EXEs or DLLs while JAR files are stored in the ZIP file format.
Source of Information -> 5- Assemblies

Categories