how to get java source code from war file? - java

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.

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.

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.

Why JAR Files Do Not Contain Documentation?

I'm in the process of writing a small Java library that contains a related code that I usually include in most of my android app. I decided to export the library as a jar file then drop the file in the libs folder of my future projects.
Using Android Studio:
I created a Java Library module and put my code in it. And I added some comments to some of the method, following this.
Then, I ran the jar task in gradle, which gave me the .jar file in build/libs directory of my module.
Now, when I used this jar in one of my android apps, Everything works as expected, except the Doc part. When I hover over the classes and methods of my library, I don't see the Doc comments that I wrote.
Q1: Am I missing another step?
Q2: Are jar files supposed to have no comments?
The javadocs are the documents that are generated from the javadoc comments in your source code. They are not part of a normal JAR file because that would unnecessarily bloat the JAR files ... with stuff that someone running to code doesn't need.
The javadocs can be generated by a Gradle task, by the javadoc command (if you have a Java SDK installed) and by various other tools. You can then read them using a web browser.
On the other hand, IDEs can often render the javadoc comments in source code and display them as pop-ups, etcetera. (Some people would call this "javadocs", but I think that is an overstatement, since you typically can't navigate the documentation ... like you can with read javadoc documents.)
In order to render the javadoc comments, the IDE needs the source code. JAR files don't (normally) contain any source code or javadocs. Instead, the normal way to deal with this is to tell the IDE where the source code is, either by pointing it at a source code directory, a ZIP file containing source code, or URL for downloading the source code.
(I don't use Android Studio, so I can tell you exactly how to do this. However, I imagine that the IDE's online help explains how to do it ...)
It seems that your end goal here is to distribute your libraries in a way that allows programmers to see the javadoc comments.
The simple way to do that is to distribute source code. This Q&A describes how to get Gradle to generate a separate archive containing the source code, or add the source code to the JAR containing your compiled code1.
If that isn't acceptable, you may need to generate the javadocs as HTML2 and provide the HTML tree as a separate ZIP file that a programmer can unzip and read with a web browser. Alternatively, put the javadocs up on a website.
1 - I would not recommend this. People who just want to use the JAR as a binary are liable to complain about "bloat".
2 - If neither providing source code or javadoc HTML documentation is acceptable, I don't think there is a pragmatic solution.
There is a separate Gradle task to generate javadoc. Try adding the following:
task javadocJar(type: Jar, dependsOn:javadoc) {
classifier = 'javadoc'
from javadoc.destinationDir }
And then run:
gradle javadocJar
See if that helps.
In addition to the above, you can try and add the following to make to generate a single jar with both compiled classes and javadoc:
jar {
from javadoc.destinationDir
}
jar.dependsOn javadoc
I don't know if that's the right decision to bundle everything in the same jar. I prefer keeping the jars separate and maybe find another way to make the IDE use the javadoc jar file. Maybe try adding the javadoc jar as another dependency of the module.
Yes This is possible
Hi, This is possible but with a small change like in the jar file.
First of all, from a code point of view jar file contains only compiled ".class" files and not source files ".java"
So if you need a doc to be applied with a jar by this I mean not the index.html which gets created but the comment that appears whenever a person uses the jar API and calls a method with a suggestion.
Example :
For that, we need to also add a source file while generating .jar file.
Steps for the same:
Type comments/java docs in code
Generate Docs
This will create a doc folder in project folder
Now create jar file
Make sure you choose this option as shown below
Almost done just test it by importing jar to another project and it should the suggestions as per docs
Very Important this can be harmful as you are including source files.java in your jar so before making make sure if you need this or not.!!!!
Hope this gave your answer
Any questions you can contact me over: VaibhavMojidra.com

Copying files during compilation process

I use builder for my buildprocess.
I have some java classes in my src directory as well as some *.qvto files. These files just need to be copied to target/classes. However, this should be done as part of the compilation process, as these files represent some part of code that does not need to be compiled. Currently, I am stuck and do not know how to proceed.
My compilation looks like this at the moment:
compile.with LIBS, projects('BPEL', 'ProcessModel', 'SharedState_ebBP')
However, I want to copy all *.qvto files from src/main/java to target/classes. And only, if they have been changed. Do you have an idea, how I can achieve this?
Assuming that you are using the Apache buildr tool, then I think the solution is to move the files to src/main/resources and they will be copied automatically by the "compile" step.
The buildr Quick Start page explains this.
Assuming that you use Maven as a building tool it needs to move all .qvto files to src/main/resources directory.

Categories

Resources