make jar file in different folder - java

when making jar file in java, how to make jar file in a different folder where other files are not located?
I have two folders f1&f2
in f1/ft1/ft2/ft3 I have the java code in f2/ft1 I have few csv fine files to be used in the java code, and I have to create jar in f2/ft2. how to do it?

Related

Java class can't read the jar file

i'm using eclipse noen and i've created a jar file contains only one java file. but when I added to the application library it still can't be seen.
Can you post the strutcture or an example of the contents of the JAR file?
JAR files should contain compiled java classes, not ".java" files.
If you are using Eclipse, create the JAR file by exporting the project.
Reference: https://help.eclipse.org/mars/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2Ftasks%2Ftasks-33.htm

Theoretical sequence: how is a .jar file created?

How are .jars created?
I know that an IDE like Eclipse can create Bytecode (.class) from developed Sourcecode (.java). And it can under "Export" create an .jar.
And know i want to know: Is an .jar created:
direct from Sourcecode?
from Bytecode which was copied?
with a totally other technique?
Source code files (.java files) are compiled to bytecode by Java Compiler. Bytecode is then stored in .class files.
These files are then packed together using jar tool to create JAR (Java Archive) file. JAR file is a zip archive usually containing:
.class files,
jar manifest
application resources
You can read more about Jar files on oficial documentation: https://docs.oracle.com/javase/tutorial/deployment/jar/basicsindex.html
A .jar is just an archive of .class files along with associated resources (if any) and metadata (if any). In fact, "jar" means Java ARchive, and the file format is the same as .zip files. From the Oracle JAR file overview :
JAR consists of a zip archive, as defined by PKWARE, containing a manifest file and potentially signature files, as defined in the JAR File Specification.
So source code is compiled to .class files (bytecode) which is then wrapped up in a .jar archive via the jar tool (or anything else that can correctly create .jar files per the spec).

How to Compile .java files into .jar file? [duplicate]

This question already has answers here:
How do I make a JAR from a .java file?
(8 answers)
Closed 6 years ago.
I just have some (134) .java source files with me and I'm pretty sure that contains all the necessary code!
I want some quick tutorial to compile this program into a .jar file.
(I'm on a windows platform btw)
I have tried javac and jar commands. I got my .jar file but it's not opening!!!
Thanks in advance!
Best practice is to make use of Ant/Maven/Gradle kind of build program tools that can take care of creating Jar files.
Other way is to just make use of Eclipse's feature for exporting the project as a light weight Jar file or Runnable Jar file(which includes dependencies).
Place all the files you want to include in the JAR file inside a
single folder.
Open the command prompt in Admin Mode
Navigate to the folder where you stored your files.
Set the path to the directory of the JDK bin. You will need to run
the jar.exe utility to create a JAR file, and that file is located
in the bin directory.
Create the JAR file.
The format of the command line for creating the
JAR file looks like this: jar cf 'jar-file'.jar input-file(s)
You can use WINRAR.
right click on file (put inside all your .java files) and compile by using winrar;
choose format .zip (important)
and save filename.jar (important)

What is a class file? What is in that file?

I know this probably won't matter to most beginners who just want to write app and that they will probably know it as they do more in Java, but it's been six months learning Java, I have no idea what this is which is the very first chapter of my Java book. So I guess I should ask.
From what I read on the book, a file class contains source code which compiler translate using resources from Java library. And it can only be created when all syntax error is fixed.
So is it just a normal file we store in our computer with a .java ending?
And also where is Java library? And how big is it?
When using java, you will encounter a few different types of files:
.java file: These are the text files where source code lives. Those .java files are compiled to .class files.
.class file: This is byte code compiled by the javac program. The javac program creates these files from .java files. The java classloader loads classes from these files and not from the .java files.
.jar file: This is what you're referring to as a java library. It is a collection of .class files, .properties files, and other files. It's just a zip file with the extension changed to .jar
.war file: This is a web application archive. It's very similar to a .jar file. It contains classes in /WEB-INF/classes and jars in /WEB-INF/LIB
.ear file: This is a way to package multiple wars into one file.
.properties file: java.util.Properties can be read easily from these. It's a text file, with each line in the format of key=value.

Accessing a .jpg file that is in a Jar

I have a question about accessing a .jpg file that is contained within a ant generated java jar file. My java project contains Java code and some .jpg files that I use. I want to be able to reference that .jpg file and be able to copy from jar to a directory.
Is there any way to that or do I always have to keep the jar outside of the jar file?
You can import the jar as a library into your project and access the resources directory or wherever you placed the JPG if you want to reference it directly from your project.
Jar files are just .zip's with a manifest. If you know where in the jar file the jpg is place, you just need to unzip the jar and copy the jpg to the desired location.

Categories

Resources