how to create jar file from source code - java

I am beginner in java programming. I have source code of jacorb and need to change a .java file and recreate jacorb.jar. I change java file but how to create jar file? Please guide me in details

Depends on how you're working.. In Eclipse, use the Export - Java - JAR file option. For commandline it's the jar command you want.

Related

How to change the CodeRunner .class file creation from same working directory of the java file in vs code?

I am currently using vs code to run my java files.
But when I use coderunner and run my java file it creates a .class file for every class in the same directory of my java file. Which gets messy how can I get rid of that?
I am ok with uninstalling the coderunner but in that case the java file errors doesn't show up properly and build fails.
Uninstall Code Runner and choose Java: Create New Project from Command Palette.
Create a no build tools project and coding in .java files which stored in folder src, then all the generated .class files will be stored in folder bin by default.
About the compilation errors, please post code snippets for further solution.
Install Test Runner for Java or Extension Pack for Java extension both by Microsoft. Better would be to go with an extension pack.

Create a jar file using compiled class files and an existing MANIFEST.MF file

Is it possible to take existing .class files and a MANIFEST.MF to create a jar file?
Is there a library that can create a "valid" jar-file? I tried it manually and it didn't work (using 7zip).
ERROR: "Invalid or corrupt jar file"
If everything has been compiled before, it should (in my understanding) theoretically work, if you create a new zip file, put all the files in it in the original structure and then rename it to "jar".
My idea is to program something like this with java code. A solution where I could add a file to an existing jar, would also be ok.
If you're interested in why I want to use this, look at my initial question: Compile javacode out of a running java accpilaction - on a system that hasn't JDK installed
Well Jar -cf
Try the jar command in $JAVA_HOME/bin
$JAVA_HOME is the path to you JRE/JDK installation

Run Java project (built in eclipse) using Batch File

I want to run my java project which i've built using Eclipse IDE. Now my goal is to create a batch file which will execute my project with one click. i referred question , but didn't get any idea. Please give me a solution. Thanks in advance.
Because you are working in eclipse, this makes things easier. First, export the whole program as a executable jar. You can do this by going to Files>Export and then in the pop up go to Java>Runnable Jar. Then follow the steps required to make it. Next you make a .bat file and write the following code.
start javaw -jar NameOfJar.jar
Make sure that you put the file in the same directory as your jar. Now you should be able and click on the .bat and execute the program!
1.Open Notepad & write
#echo off
javac YOUR_JAVA_FILENAME.java
java YOUR_JAVA_FILENAME
2.Save-As executeJavaProgram.bat
Make sure that YOUR_JAVA_FILENAME.java resides with your batch file and java path is set in environment variable.
3 . Double click on batch file.
Follow this tutorial to create a jar file of your eclipse project.
After doing it create a batch file in the same folder where you exported the jar with the command: java -jar yourjar.jar
Create jar file using eclipse i.e right click on your project select export jar file then provide file name to store your jar file. In this file eclipse will keep all .class file only and in META-INF folder main class definition if your are creating executable jar file.

creating and running jar file with source

I write a very simple java program with two classes: Business and Main.
I want to create a jar such that if I email it to someone they can:
run the program (i.e. run the jar)
open the jar to view the source code.
the code can run on mac or windows
I have been using IDEs for so long I have forgotten how to do this.
I am using netbeans 7.x
EDIT:
I found the following way on Netbeans:
properties > packaging > exclude from jar file :: delete **/*.java
But when I try to execute the jar using
java -jar mybusiness.jar
it says
no main manifest attribute, in mybusiness.jar
But note that my jar has a main class. Am I missing a manifest file?
You can export a JAR file that includes the source code using Netbeans:
Right click on the project and select properties
Build -> Packaging
Remove Java files from the excluded files. And select build jar after compiling
It will create the jar file that includes the source code if it successfully compiles.
Well surely an IDE can do this too?. Just make some text files and put the source into them and drag them into the ide's. It's java so it should automatically run on all platforms. I am not sure what the problem is here?
This link explains how do u create manifest file and how do you specify your main class in manifest file as its necessary for executing jar.

Extract source code from .jar file

Is there a way to extract the source code from an executable .jar file (Java ME)?
Use JD GUI. Open the application, drag and drop your JAR file into it.
You can extract a jar file with the command :
jar xf filename.jar
References : Oracle's JAR documentation
I believe this can be done very easily. You can always extract the source files (Java files) of a jar file into a zip.
Steps to get sources of a jar file as a zip :
Download JAD from http://techieme.in/resources-needed/ and save it at any
location on your system.
Drag and drop the jar for which you want the sources on the JAD.
3 JAD UI will open with all the package structure in a tree format.
Click on File menu and select save jar sources.
It will save the sources as a zip with the same name as the jar.
Hope this helps.
The link is dead due to some reason so adding the link from where you can download the JDGUI
Your JAR may contain source and javadoc, in which case you can simply use jar xf my.jar to extract them.
Otherwise you can use a decompiler as mentioned in adarshr's answer:
Use JD GUI. Open the application, drag
and drop your JAR file into it.
I know it's an old question Still thought it would help someone
1) Go to your jar file's folder.
2) change it's extension to .zip.
3) You are good to go and can easily extract it by just double clicking it.
Note: I tested this in MAC, it works. Hopefully it will work on windows too.
Do the following on your linux box where java works (if u like the terminal way of doing things)
cd ~
mkdir decompiled_code && cd decompiled_code
// download jar procyon from https://drive.google.com/file/d/1yC2gJhmLoyE8royCph5dLEncgkNZXj58/view?usp=sharing
java -jar procyon-decompiler-0.5.36.jar /Path/to/your/jar -o .
NOTE : as #Richard commented "this may be illegal depending on whether you own the copyright to the jar, the country you live in and your purpose for doing it."
Steps to get sources of a jar file as a zip :
Download JD-GUI from http://java-decompiler.github.io/ and save it
at any location on your system.
Drag and drop the jar or open .jar file for which you want the
sources on the JD.
Java Decompiler will open with all the package structure in a tree
format.
Click on File menu and select save jar sources. It will save
the sources as a zip with the same name as the jar.
Example:-
We can use Eclipse IDE for Java EE Developers as well for update/extract code if require.
From eclipse chose Import Jar and then select jar which you need. Follow instruction as per image below
AndroChef Java Decompiler produces very good code that you can use directly in your projects...
Above tools extract the jar. Also there are certain other tools and commands to extract the jar.
But AFAIK you cant get the java code in case code has been obfuscated.
suppose your JAR file is in C:\Documents and Settings\mmeher\Desktop\jar and the JAR file name is xx.jar, then write the below two commands in command prompt:
1> cd C:\Documents and Settings\mmeher\Desktop\jar
2> jar xf xx.jar
-Covert .jar file to .zip (In windows just change the extension)
-Unzip the .zip folder
-You will get complete .java files

Categories

Resources