Turning Java code into a program? [duplicate] - java

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.

Related

Are jar files decompressed on the fly [duplicate]

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.

How to create .jar files with additional resources without using an IDE [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
How can I create a .jar file which includes both class files and some additional resources(namely images) without the use of an IDE.
You can use maven or ant to reach this goal.
In order to create a jar of your project you need to the following command:
jar cf jar-file input-file(s)
first make sure that running in command prompt or terminal jar is not going to tell you "command not found".
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.
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.
Let's assume you have a directory named "My Project" which contains all your java packages, classes, resources, etc. You need to jar "My Project" hence, you should do the following:
Navigate to the parent of the directory that contains "My Project" using command prompt (in windows) and terminal (in OSX or linux)
execute the jar command as follow (in windows - double quotes around My Project is because of space in directory name, you might not need it)
jar cf MyProject.jar "My Project"
Above command will give you the directory in jar.
For more info on creating jar see oracle documentation.
I can only link you to the oracle page : https://docs.oracle.com/javase/tutorial/deployment/jar/build.html
The command you are looking for seems to be :
jar cvf TicTacToe.jar TicTacToe.class audio images
Where audio and images are resource folders.
Good luck,

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.

is there a way to know java packages in a jar file? [duplicate]

This question already has answers here:
How do I find the packages defined in a jar?
(5 answers)
Closed 8 years ago.
Is there any way to know the packages of a .jar file as I want to use "gtranslateapi-1.0" but not getting the package or class names in it.
I have also added it to my libraries in netbeans 8.0
You can see it here: https://code.google.com/p/java-google-translate-text-to-speech/downloads/list
please help, thanks in advance !!
jar is just a zip.so if you want to know what is packed into a jar file, you may unzip it (using either your favourite zip tool or jar itself e.g jar -t to list the contents). hint jar without args gives you a list of options
in netbeans you can easily see packages and classes .or you can rename .jar to .zip and open in compress program like winrar
in netbeans you can expand jar easily.add jar to libries and expand it .this is your jar file

How to make executable file independent of Eclipse [duplicate]

This question already has answers here:
How can I convert my Java program to an .exe file?
(16 answers)
Closed 8 years ago.
I have one library, one txt file for import some things with it and one output file to show the result. Only have one java file without any interface. With eclipse all is running well - I am writing into the input file and then it shows the result into the output file. Now I want to make this all independent program (without need to run with eclipse).
How to make it?
An easier way is to make a runnable JAR file.
Go into eclipse choose file > export > runnable JAR.
It will then ask you to choose a class for a launch configuration. Choose the class that has the main method for the desired program. After that you will have a file that will run (like a .exe but just a different format).
If can't get it to work with your input and output files, thats because eclipse has this weird thing that sometimes occurs regarding the location of external sources when exporting to JAR. To fix it, just put the files instead of the src (source) folder, one folder up in the directory. For example, if this is your current directory: C:\workspace\myprogram\src put it in C:\workspace\myprogram
Hope this helps.
The only thing that eclipse is is a software that facilitates progtammi g. But what find the programs is the jvm-java virtual machine. To get to the point eclipse does have a option to convert it to a .jar file which the extension a java program uses to be "independent" from eclipse and open up by double clicking. The way you do this is you right click on the class you want to make independent and I think there should an option that says export to .jar and that should do the trick! Hope this was helpful

Categories