This question already has answers here:
Closed 12 years ago.
Possible Duplicates:
Where on the file system was my java class loaded from?
How do I get the directory that the currently executing jar file is in?
How can i know .jar file directory within my program
i want to use this directory to create another files
in same directory which user save this .jar file
I don't think you can easily find out where the JAR file is, but you can find out which directory your program is being run from:
File cwdFile = new File (".");
String cwd = cwdFile.getAbsolutePath();
This only works if the user actually runs the JAR file from the directory it's in:
java -jar MyExectuableJar.jar
If they run it from another directory, like this:
java -jar /usr/bin/java/MyExecutableJar.jar
then you'll get whichever directory the user happens to be in at the time.
Related
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)
This question already has answers here:
How to make an executable JAR file?
(5 answers)
Closed 6 years ago.
I have taken an executable jar file from server to my local system and then I have unrar it through win rar and then I have made some changes in the .class file now i again want to make executable jar file of the folder that get created when I initially unrar it please advise how can i create executable jar file again
folks please advise with context to creating an executable jar file
Let's say that all your project file's are in folder named MyApp
then
open command prompt
navigate to directory where MyApp is placed using cd
execute command jar cvf MyApp.jar MyApp
EDIT
(To make this jar executable:)
create a file named MANIFEST.MF and put it in META-INF folder
META-INF/MANIFEST.MF
Manifest-Version: 1.0
Main-Class: com.somepackage.MyApp
OR you can simply use Launch4j to wrap up your program into an executable: http://launch4j.sourceforge.net/
This question already has answers here:
How to make an executable JAR file?
(5 answers)
Closed 6 years ago.
I am learning Java, and I have been programming an 80s-styled text based game. I have experience with coding so I know I will be done soon. Just so I knew how, I decided to make a .jar file. It created fine, but I have a problem: I want it to run and work in the command prompt, just like if I were using the "java FileName" command. Is there any way to do this? I want to distribute it so that for Windows, it runs in the command prompt exclusively (Apple and Linux is down the line).
Edit/Summary: To some people, this might be a duplicate of a question. It's not for these simple reasons: I know how to create a .jar file. I am trying to figure out how to create a specific type of .jar file that opens and runs in the command prompt.
I assume that you have compiled your .java files into class files. If so, than do the following:
Make a manifest file called manifest.txt and inside the file mention the class name where the main class resides. For example:
Main-Class: com.A.B.MyMainClass
com.A.B.MyMainClass is example here. You should mention your package names.
Put the above line in the manifest.txt file
Than, suppose you have your class files in c:\test\classes\ folder and suppose your class files in this folder are a.class and b.class and MyMainClass.class
Now execute the command:
jar -cvfm MyExecutable.jar manifest.txt c:/test/classes/*.class
It will create an exectable jar for you which you can run in Windows and Mac and also in Linux.
To execute the jar file:
java -jar MyExecutable.jar in the command prompt.
Use this to run your jar on command line:
java -jar <jar-name>.jar
Assuming that you have java installed on the system
Using eclipse -
http://help.eclipse.org/mars/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2Ftasks%2Ftasks-33.htm
Using Command line -
https://docs.oracle.com/javase/tutorial/deployment/jar/build.html
To run the jar file -
http://docs.oracle.com/javase/tutorial/deployment/jar/appman.html
This question already has answers here:
Specifying classpath for a jar
(5 answers)
Closed 8 years ago.
I am not familiar with Java and I am trying to run a piece of Java code packaged as jar file.
On Windows command line, I specify the CLASSPATH to my jar folder like this:
set CLASSPATH="D:\jarFolder"
And there's a test.jar file in that folder. But when I run this
java -jar test.jar
it still failed with this error:
Unable to access jarfile test.jar
I can run the test.jar by specify the full path. But I want to know why the CLASSPATH doesn't work. My understanding is, it tells the java runtime where to locate the jar file.
Jarfiles have to be on the classpath explicitly. Specifying a classpath dir for a jarfile is not sufficient.
See the related Oracle docs on the Java command line:
When you use this option, the JAR file is the source of all user
classes, and other user class path settings are ignored.
Try to use
java -jar D:\jarFolder\test.jar
If you know main class name of your entry point of jar. You can use
set CLASSPATH="D:\jarFolder"
java yourpackage.yourEntryPoint
You can learn your entry point of your jar file using a zip program to extract jar file and reading file entry point section of manifest.mf file.
When you use -jar option, you need to specify which jar. Think of it this way. If you do not give jar name, which jar java should execute? You may have 10,20 may be 100 jars in your class path.
But if you give class name, then java searches among jars and execute correct one.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Corrupt jar file
I made a game in Eclipse and packaged all the class files in test.jar. When I attempt to run it, it gives me the error:
Error: invalid or corrupted jar file.
I opened the .jar file using Winrar, and everything is in there.
I have looked for a similar question on Stack OverFlow, but didn't find any. Can you explain to me how I can solve this error?
FOR EVERYONE WITH THIS PROBLEM ,SOLUTION:
Go to file, export, export as a Runnable Jar, Select your main class, finish.
If you want to run your program in cmd, create a new textfile, and type java -jar project.jar with project = name of your program.
Save this as Run.bat
then open the Run.bat, it will automatically open project.jar
Thats it, it worked for me!
How you place your classes to tar.jar? A valid jar file must follow some rules it requires. You'd best generate them by your Eclipse tools. click "export" -> "jar" and select the right entry of your game. Good Luck.