How to use mailed .class files in eclipse - java

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.

Related

Java - Export and build eclipse project from target folder

How can I export files to eclipse project from the target folder?
I lost my project files. All I have is a target folder with binary .class files and static files in html.
Can you somehow build a project out of it?
Not possible. You can't go back to the original source files from just class files. You could decompile them which means you get code stripped of all names and comments except for method/fieldnames, code is mangled and worse, and probably broken. If you must - Here are some decompilers. Not sure if that site truly works (sites that only load over plain jane http are rather suspicious), but even if not, just search the web for any of the decompilers named there and run it on those class files.
As I said, the end result will look horrible, but it's the best you can do.

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.

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 get java source code from war file?

I do not have the latest source code but have the war(up to date) file deployed on server.
Please suggest the best ways to
1) Retrieve source code from war/ear
2) Compare & Merge/update the available source code with the code present in war/ear but missing in available source code(I am using ECLIPSE IDE)
Thanks in advance
War files are basically zip files, so they are easy to extract. (using unzip or just renaming the file)
Next you could use a Java decompiler like JD.
But you won't get the original Java code as the compiler does a lot of optimization.
But it should give you a good starting point
Once you've extracted the classes from the EAR/WAR/Jars, use JAD to decompile the code you're interested in to get back to the source: http://varaneckas.com/jad/
I'm not sure there's any out-of-the-box tool that is going to compare/diff your original source with the decompiled source produced from something like JAD though. Also bear in mind, decompiling classes back to source is not going to produce source that looks identical to the original source - code style is going to be different, maybe even some structure of the code. It's going to be difficult to do a diff between the original source and decompiled source.
If you have the original source but not the source for the code that is currently deployed, maybe a better question is to ask 'why not'? If there's something missing in your build process where you are not tracking what source is being used for each build, maybe this is an easier issue to address moving forward, rather than trying to do something clumsy and error prone like a diff between some other source and decompiled source?
The exact answer: it is not possible to get the original source code (.java files) from a war as opposed to a jar (java archive). When you create a jar file, you can decide if you want to include the .java files. Only a java decompiler can help, see the other answers.
Using JD GUI you can the source code with java code, but you'll need to
Inside the war folder, under specific module - Based on your project hierarchy (if maven project -these config will be available in Pom.xml - it will define which path and what jar name)
you will have the Core JAR files of each module.
Open those jar files using any decompiler , you will be able to find the class/java files in it..
Here is your complete solution.
If while creating war file, you have to make sure that you have added the code.
Otherwise, do one thing.
Deploy the war file in your server, may be on tomcat server.
To deploy the war file, you need to put that war file in webapps folder (C:\ASHIS_CODE\apache-tomcat-9.0.65\webapps).
enter image description here
After putting, you need to restart your tomcat server.
Then one folder with same name as of your war file name, will be created in side webapps folder.
Open that folder in your eclipse or any other ide, that folder contains your project code.
** Hope this clears your issue.

Create project from .jar file

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.

Categories

Resources