This question already has answers here:
How does a jar file get executed? Do the classes get extracted somewhere?
(2 answers)
Closed 3 years ago.
Take a standalone executable jar file for example, in which we generate from our application with all the dependencies etc. My understanding is that this file contains all the classes etc. compressed.
When we execute this jar file via the command line as follows java -jar myjar.jar , is this being decompressed on the fly? Does the interpreter first decompress everything before executing or how does this work exactly?
We already have one answer to similar question here :
How does a jar file get executed? Do the classes get extracted somewhere?
The JVM is capable of loading classes or files from a jar file without extracting the jar to temp files.This functionality is also available to you in the standard library, see the JarFile for more information.
So no, the JVM does not extract a jar to temp files, classes (and resources) are simply loaded on demand.
We can also check if the jar gets extracted or not by executing the command "java -jar myjar.jar" and check the folder where jar is located if there is any extraction while executing the program.
Related
This question already has an answer here:
Executing a python file from within JAR
(1 answer)
Closed 5 years ago.
So I'm using IntelliJ IDEA and I've got my structure as follows:
So firstly, I want to be able to package the res folder with all my scripts inside the jar, and then when I run my program it can access the python scripts from inside the jar itself.
So far when I try running things on res/test.py it can't find the file.
EDIT: I've taken a look at an article which suggests getting the temporary files location using System.getProperty("java.io.tmpdir") and maybe copying the files to there using Class.getResourceAsStream("/path/to/file")
I'm not sure however if Class#getResourceAsStream is suitable for getting it from inside the jar to write it to the temporary location.
The code works when you check the file exists prior to executing the Process:
File f = new File("res/test.py");
System.out.println(f.exists()); // true
This question already has an answer here:
How do I compile a java file that has jar dependencies?
(1 answer)
Closed 8 years ago.
Background: Running on an ec2 instance not eclipse.
I have a java program that has external Jar dependencies. I want to create an executable jar file for this java program (being able to package the external jar files into this jar file would be icing on the cake)
The problem is that for me to use the java cf or cmf command I need to have a .class file. But, when I try javac HKP.java it throws a bunch of errors saying "xyz does not exist or cannot recognize symbol" which I'm guessing is because it has no idea about the external jar dependencies.
I have tried doing javac -sourcepath /home/ec2-user/apps/HKP/lib/ HKP.java, but it throws the exact same errors as before.
EDIT I have tried javac -classpath file1.jar, file2.jar, file3.jar HKP.java -I have also tried this with ";" in between. both return errors saying -bash file2.jar command not found, so instead of continuing the argument, bash is recognizing the multiple jar files as commands.This is the initial problem I had that caused me to post here.
Any help is much appreciated. Thank you!
If you want to compile a java source file into a class, you need to provide all classes which are used in that code on the classpath of the compiler.
If the classes come from (already compiled) external JARs, you typically do that by specifying javac -classpath with a list of JAR files, separated by : (on Linux). But you should really think about using an IDE or at least a maven build file (Which has the benefit, it can even download those JARs for you).
Both (Eclipse IDE and Maven build system) can also generate a ueber-jar with the external classes in there for easy execution.
try to Add the path in system variable and restart your computer.
This question already has answers here:
How do I create an .exe for a Java program? [duplicate]
(7 answers)
Closed 9 years ago.
I have created a project in java Netbeans and I want to create an .exe or .jar file that can be run on any other systems.
my project have a Main.java class and an other class GetRules.java .
the .exe file should be such that it can be run in any folder that contain train.txt as input and create Model.txt as output
how can I do this?
what you are looking for is an executable file which can execute you project
calling you main class
so here is what you can do
step 1 : collect all the files , class files
step 2 : create a jar file collecting all the files from your project
step 3 : this.jar file is executable on any environment with a JVM available
you will have .jar file , rather than .exe file , but serves you the purpose
you can refer this link to learn how to create jar files
Java programs are not compiled into exe files but Java byte code which is usually stored in jar containers. exe files run on Windows systems only, but Java byte code is platform independent.
What you could do is use a wrapper like Launch4j. It wraps the jar file into an exe file.
This question already has answers here:
How do I create a ZIP file in Java?
(2 answers)
Closed 9 years ago.
Is it possible to compile class file to jar file not using jar -cvf options.
I want to compile jar files by source code.
Cant somebody show me some example codes
A JAR simply is a ZIP file.
While I am not quite what you are trying to achieve, I can give you these hints:
Compiling: javac Hello.java
Creating a JAR: zip Hello.jar Hello.class
If you want to have a JAR containing your sources, you could as well run the above command with: zip Hello.jar Hello.java
Also note that, if you are using a build tool like for example maven, there are various plugins for suchs tasks such as 'assembly' (for maven).
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to make executable program in java?
I would like to know how I can open my GUI program without going through the terminal? It being independent, and simply having to click on an icon and it open it.
You need to build a jar file, you can do this with the following command
jar cf jar-file input-file(s)
The options and arguments used in this command are:
The c option indicates that you want to create a JAR file.
The f option indicates that you want the output to go to a file rather than to stdout.
jar-file is the name that you want the resulting JAR file to have. You can use any filename for a JAR file. By convention, JAR filenames are given a .jar extension, though this is not required.
The input-file(s) argument is a space-separated list of one or more files that you want to include in your JAR file.
The input-file(s) argument can contain the wildcard * symbol. If any of the "input-files" are directories, the contents of those directories are added to the JAR archive recursively.
http://docs.oracle.com/javase/tutorial/deployment/jar/build.html
I think you're looking for a jar file. See the Creating a JAR file Java tutorial.