Create project from .jar file - java

I will explain what I need to do, in fact I found this answer Restoring code from JAR which is pretty close to my needs but with a key difference.
What I have is a folder with many jar files which include the .class but also the .java files of an application, I would like to restore the project into any IDE such as Eclipse or netbeans.
In order to make it clearer, the structure of what I have is the following:
Main_directory
File_1.jar
file_1_1.java
file_1_1.class
file_1_2.java
file_1_2.class
File_2.jar
file_2_1.java
file_2_1.class
file_2_2.java
file_2_2.class
I would really appreciate any suggestions but I would like to avoid using decompilers.
Best.-

.jar files can be opened and files extracted with many programs, such as tar, winrar, etc. Your best bet, considering these .jar files contain the actual .java files is to extract them with such a tool, create a new project in your ide(eclipse) and then import the .java files to the new project.

Related

How to edit .java files imported in Eclipse from a .jar file

I have a question, perhaps it was already answered, but i didn't manage to find it and I appologize if the solution already exists (let me know if it is before deleting my thread).
Problem is:
I have created a program on another PC and exported it from eclipse as a .jar file. It works on my main PC when I double click on it but when I import it in Eclipse I can't find the .java file. So i can't edit it.
What I have done so far:
In eclipse I have created a new empty project
I have right clicked,import, archive file, selected the .class files that eclipse sees, but when I am in the Project Explorer in Eclipse I can't find the .java file where the main is. I mean I can click run as a program and it works, but there is no .java file, only .class files. What am I doing wrong?
That cranes.class should be cranes.java. At least on my other PC it is.
Program works fine, but I can't edit it on my main PC. What am I doing wrong?
Thanks and best regards
You need to select the Export Java source files and resources option while creating the jar file and then your Java files will be available on importing the project from the jar file.
This is similar to how you use other libraries. You depend on the Jar file which contains class bytecode (compiled) of java code. You can't edit any of such files directly in the project you are using it. Thought you can always extends functionalities in your current project using simple inheritance concepts.
If you think such functionalities are trivial you should prefer to change in the original project rebuild the jar and use the newer version of jar.
However if you feel similar things for 3rd party library you can
always make changes after taking fork from those library source
code (if open source) and build and use your own version or go
ahead and raise pull request if you are confident about your
changes.
Mostly when you build a jar file, all you have in it are .class files; these are the result of compiling .java files, and so are not editable with text editors.
You CAN create a jar file that contains .java (also known as source) files, and even a jar file that contains both .java and .class files, but if you ask eclipse to create a jar file, by default it is just going to put .class files and files from resource folders in it, not .java files.
Assuming from the question, the jar is a library created by OP, by compiling java files into class files and packing/exporting them. Although the class files can't be edited in any IDE, they can be de-compiled into Java files by using third-party applications.
I personally use IntelliJ for this de-compiling source files authored by me
Note: Although this gives OP the desired functionality, it may lead to violations if the classes are Copyrighted.
As IntelliJ states, they neither encourage nor discourage de-compiling class files and the decision is purely to the user's discretion.
EDIT: It is always recommended to use the original source files for editing. Try to host them on git so that it may be retrieved anytime required
It may be simpler to not use eclipse but jar/zip/tar your project directory on the one computer and simply extract it onto the other, then open that folder as a new project in Eclipse.
Best is the suggestion from #SanjayBharathi to use git and clone the repo on your other machine.

is there a way to make changes in jar which doesnot have source code in it and rebuild it as a jar in eclipse?

Is there a way to make changes in jar which doesnot have source code in it and rebuild it with this change as a jar in eclipse.
You can look into using Java decompilers; in order to turn the .class files within the JAR archive back into .java source code. The process and some tools for that are outlined here.
But: understand that *decompiling" can be a tough business! Plus: there is always the question if the licence terms of the library you are using allows you to do that. Being able to do something isn't the same as being allowed to do that!
Finally: keep in mind that a JAR is just a deployment artifact. A lot of libraries are open source, and you can most often download that source and build the corresponding JARs completely on your own.

Importing .java and .class file gone wrong in eclipse

Recently I worked on a project on a mac, using eclipse. When I was done with the project I copied certain .java files and their corresponding .class files onto a USB drive and brought it to my personal computer. When I try to use the import feature on eclipse and import everything, my main method is not recognized by eclipse. It will work if I create a new eclipse "class" by the name I have in the program and copy paste the code. I have many classes so I can not do this for each one. Is there any way for me to change all .java/.class files into files that will be read by the system?
Thanks
An eclipse project contains a lot of meta information. Just open the project directory in a file browser, and have a closer look. There are files like .classpath for example.
When you just copy your source code, you leave all the meta information behind!
You should either copy the complete project directory, or simply use the export task to create a ZIP file of your project. (to later import that in other systems).
There is one other way you can do this. Just create a new project with the SAME EXACT PROJECT NAME that you did at school or wherever. After you create your project, go to your file explorer and navigate to the following place:
<"Directory to work-space">/<"Project Name">/src/
For ex: in Windows, it is Generally:
C:/Users/your_name/workspace/projectName/src
In here, copy all your ".java" files.
Now go to eclipse, right click on your project and hit refresh. You will find all your .java files there. Right-click on your project and hit "Run"->"Run as a Java Project". It should run! hope this helps.
PS: You do not need your class if you are executing it in Eclipse. Eclipse will create its own class files.

How to use mailed .class files in eclipse

So a friend of mine told me I could mail my java projects to myself (the .class files) and then just put them into my workspace (yes I know about github, but having trouble with it, looking into it), though when I paste them into my workspace in the correct package, the code does not change.
Even making an entire new package (which would be required since I mailed 2 projects, from which only one was on my primary computer) did not help, as the package remains empty.
Yes I've tried the import option, didn't work (yes I'm fairly new to eclipse)
Any help is appreciated
The .class files are binary executables that are compiled from the .java files. If you want to transfer an entire Eclipse project from one computer to another, you could zip the project from the top-level in your Eclipse workspace, email it, unzip it on the target computer, and then import it as an existing project into Eclipse. You can unzip it anywhere, but you might prefer to put the project into the Eclipse workspace on the target computer as a good convention.
Note: I would recommend cleaning the project before zipping it so that you don't transfer the .class files that are going to be rebuilt anyway. That will make the zip archive smaller.
You can mail Java projects to yourself, but you want to send the source (.java) files to yourself. The .class files contain compiled byte-code and are generated from the source. Zip up the entire project folder and email it to yourself or place it on a thumb drive.

Appending folders and files (and overwriting them occasionally) using java only

How can I splice files (ANY files, pngs, classes you name it) and folders into a pre-existing jar file ONLY using java code. That means no jar.exe utility. I am planning to make a program that places files/folders into a specific jar file. I have looked at several tutorials on java.util.jar and other jar managing modules but none of them seem to be right for me. Do I have to do this from scratch? Here is an over view of what I am thinking:
Take the desired files and folders from within a specific folder
open the target jar and place the files and folders within, overwriting other files if need be
I'm thinking I have to decompile the jar and then repackage it (ONLY USING JAVA CODE). I don't know what to do.

Categories

Resources