Recover overwritten JAVA file - java

I was making a jar file but accidentally wrote TimerV1.java instead of TimerV1.jar and as a result, my whole source code got replaced with machine code and the sad thing is there's still something I have to edit.
I am using Notepad++ and tried searching the Roaming directory but there's nothing there.
I also tried decompiling the jar file and editing the exported source files (with JD-GUI) but when I try to compile those source files I get errors that I don't even know how to resolve.
Is there any way to recover the previous versions of my java file?

You can use JAD (Java Decompiler) but.. not all code can be reversed into source.
Try one of:
http://jd.benow.ca/
https://bitbucket.org/mstrobel/procyon/wiki/Java%20Decompiler
http://www.benf.org/other/cfr/
https://github.com/fesh0r/fernflower
http://www.javadecompilers.com/jad

I don't use Notepad++ myself but it seems there is a backup folder where older versions of edited files are kept:
C:\Users\<user>\AppData\Roaming\Notepad++\backup

There are various utilities out there that can recover deleted files, I think because typically they are still there but the OS lists that space as unallocated. But overwritten files...I wish I had a better answer but I don't know if they can be recovered, unless some version was saved somewhere.
If it was a relatively short file, maybe the best thing to do is take this as an painful opportunity to use a backup service like Carbonite or Dropbox, and use version control like Git or SVN.

Nevermind. I have used another decompiler and debugged one by one. Thanks to those who answered :)
I used Cavaj Decompiler.

Related

How to import JAR file as a project into an IDE?

I have a Maven project. After I make changes to the project, I package the project up into an Uber Jar file and upload that to my backups. I am wondering, if my computer were to break or I lose all the code to the project, can I get the project back using only the Uber Jar file or would the project be gone forever?
How can I open this Jar file as a project and view all the Java code inside of it?
It would be entirely gone; that jar file contains only class files, not your source files, and you can't 'recover' source files from class files (you can decompile them which is mostly useless for this purpose; all comments, most names, most structure - all gone. You can't feasibly continue your project with this).
As it isn't in there, it's not possible to 'open a jar file and see all java code inside it'.
You'll need to set up backups.
The proper way forward is to first set up version control; this ensures that you don't just have a backup of the latest state of your source files, but every state of it - you can travel back to any time. Protects against code you deleted by accident, and means you can freely remove code you think you no longer need without that nagging feeling of: Yeah but what if.... maybe later? - so stop commenting out stuff you don't need, just get rid of it. It also means if there's a bug, you can time travel to the exact point in time when you added the line, and you can review the changes made by others in a project (presumably you're writing this on your own for now, but at some point you'll work with more people than just yourself!)
Read up on git which is the version control system that 80%+ or so of the community uses, probably best not to spring for exotic options if you aren't familiar. There are a billion tutorials out there to find if you search the web.
Host your git on a site like github or bitbucket which therefore also takes care of backups. Alternatively, host it on your own server (it's not complicated; git is open source). If you can't do that either, just let git write to a local directory and then use e.g. backblaze or something similar to ensure that file is backed up.
Yes, you can view your code by using a decompiler. I have experience mostly with IntelliJ, and this IDE includes a decompiler of its own. In case you lose everything and have only the jar file. You can use this included decompiler to get your source back from your .class files. But instructions on doing that are a story for another question...
If you want to secure your code use GIT. A version control tool that is a must when it comes to programming. Google about it and after a few days of playing around with it, you will never worry about such things.
Right-click on your project.
Select Build Path.
Click on Configure Build Path.
Click on Libraries, select Modulepath and select Add External JARs.
Select the jar file from the required folder.
Click and Apply and Ok.

IntelliJ IDEA - Decompiling/Editing/Recompiling JAR

I realize this may seem like a completely stupid question and this question is a "wall of text", but I'm feeling like I've gotten way out of my depth here and need some guidance. I've read several SO questions on this topic, but still can't seem to find one that addresses this particular situation. (please reference update(s) at the end of this post)
BACKGROUND
Our company has an application that was built in Java and released as an executable JAR package by a developer who passed away a couple of years ago. Since then, there has been minimal need for even looking at that code, let alone making any changes. That's been really good because I do my programming in VB.NET (Visual Studio) and, while I can read and make sense of Java code, I'm not proficient in actually writing/debugging that code.
However, a recent issue has come up that forced me to try to take a look at this Java code due to some internal changes in organization and data structure. I've looked in his "src\" directory and found older versions of his original code but wasn't able to find the most recent version anywhere. I found some code that made it possible for me to extract the JAR that's currently being used to a local directory on CodeProject (JarsVB), so I've been able to look over some of the .java files when trying to figure out what query is being used for some random operation. I don't know what tool(s) the original developer used to create the project/JAR, so I've installed the IntelliJ IDEA Community Edition 2018 as an IDE, and it's worked for me so far to simply view the code so I can understand a bit about what it's doing.
PROBLEM/GOALS
Unfortunately, now there is a need for me to make a change to the Java code and update the JAR, and this is where I can't seem to make heads or tails of what I need to do. I have my local copy of the code from the "unzipped" JAR containing all the .java and .class files (along with all the other resources), but I'm not sure how to go from that to modifying the code and recompiling the executable JAR.
So, my goals here are as follows:
(properly) Decompile the existing executable JAR. (If the JarsVB solution I mentioned above did what it was supposed to do, I should already have this part handled, but if there's a better, more "integrated" way of doing it, I'd be open to that as well.)
Modify one or more .java files. (I believe I can figure out the syntax well enough to get this part done.)
Debug/test my changes.
Recompile the changes into an updated executable JAR for release. (THIS is where I'm experiencing the most confusion.)
WHAT I'VE DONE
I've been looking at the documentation for IntelliJ to try to figure out how to make this all happen, but I feel like I'm missing stuff. I set my "Project Structure" settings to point to a specific folder, but I can't seem to get it to actually build anything in my specified path.
I went into one of the .java files and made/saved a small change to test, then went to the Build menu and tried all the building options available to me: "Build Project", "Build Module", and "Rebuild Project". Each time, the event log shows that "All files are up-to-date" (even though I changed one), so I go to my output directory to see what it built, but the folder is empty.
I looked at the IntelliJ documentation for Packaging a Module into a JAR File, which says to
On the main menu, choose Build | Build Artifact
But that option is disabled in my Build menu, and when I look in the Project Structure dialog, the Artifacts section is empty, so I try to add a new setting (Add -> JAR -> From modules with dependencies...), but I'm not sure how to properly set that up either.
I tried to select a Main Class from my classes/ directory, but it wouldn't actually accept any of the .class files I selected, so I just left it blank. Then, I did a search for a MANIFEST file, but couldn't find one anywhere so I left that blank as well. The Build menu option is enabled now, but when I tried to Build Artifact, again, I get nothing in my output directory. In fact, the only files I can find that have changed are in my local working directory. I'm just dumbfounded at this point.
FINAL THOUGHTS/QUESTIONS
I've tried to provide as much detail here as I could think of about all the things I've tried and gone through to get this JAR updated, but if there's a question about anything, please let me know. I'm not looking for a "handout" and I don't expect anyone to do this for me, but I'm also not wanting to become a Java developer just for the sake of making some minor changes to an application that will eventually be replaced by a .NET application. I simply am not familiar enough with the tools or Java development in general to know how to get to where I want to be from where I am.
My decompiled source files are in a totally separate directory from the original, production JAR file because, when I recompile this time, I want to completely recreate the JAR. Am I understanding the Java development process correctly in editing one of the .java files I got from decompiling with the JarsVB and then recompiling the JAR?
Thanks in advance for any assistance in finding the resources I need to accomplish these goals.
EDIT/UPDATE
So, looking at the link in the accepted answer in another SO question, How to build jars from IntelliJ properly?, I figured out at least one part of my issue: Leaving the Main Class setting of my Artifacts configuration blank was a problem. Since I hadn't been able to select a .class file and I wasn't sure how to correctly populate that field, I hadn't given the IDE enough information to operate correctly. I entered the namespace and class (namespace.class) I found in the class file that apparently defines the main method for the application, then set the path for the MANIFISET.MF file to my output directory and tried again to Build Artifact.
This time, at least, it DID produce a JAR in my defined output directory, but it's only 1KB in size. As stated above, the source files are in a completely separate directory from the original JAR file from which they were decompiled. The output directory, too, is completely separate from the location of the original JAR file. Do I need a copy of the original JAR file in the output path for recompiling to work correctly?
I'm making progress, but I'm sure I'm overlooking something "stupid" at this point that's primarily due to my unfamiliarity with the IDE and developing Java applications in general.
UPDATE 2
Looking at another SO question - how to create jar of java package in intelliJ idea - I learned that I have to individually add the necessary files for repackaging into the JAR. However, this brings up the question, what files do I add? The linked question identifies the .class files, but when I go look at the .class files in my working directory, none of those have been updated recently, so it looks like I'm still missing a step somewhere.
Another question this brings up is, are there certain conventions for Java development of which I need to be aware when preparing my environment? I have my output path set to a completely separate folder than any working or production code, so I'm wondering if something in that setup might potentially cause issues.
As I said before, I made a small change to one of the .java files, then tried both the Build Module and Rebuild Project options, but those are still telling me that "All files are up-to-date". Even so, I tried adding just the .class files from under my classes\ directory to my Artifact configuration and tried again to Build Artifact. I got a bit larger file (approx. 5MB), but when I try to execute the JAR, it just doesn't appear to do anything, let alone actually launch the application.
I tried again by adding the root folder of my local copy of the source adding everything the root folder contains. (yes, the directory probably needs some "spring cleaning", but that's for another day)
This time, I got a much larger file this time (approx. 21MB), so I thought I might have fixed the problem. Unfortunately, no such luck. The JAR still doesn't appear to execute.
For reference, the original JAR file from which the code was decompiled is approx. 59MB in size so, either IntelliJ is doing an incredible job with compression, or there's yet another step I haven't yet found. I'm sure this is all a matter of getting my IDE configured correctly, but I just can't seem to find the right combination of settings.

How to deobfuscate jar file using mapping.txt generated by Proguard?

This may sound suspicious, but I need to deobfuscate my own app. Some time ago I published my app that I obfuscated with Proguard. Unfortunately I did not save that version of the app and changed the code a lot.
Now I need the code of that older version. I still have mapping.txt file generated by Proguard.
I have converted the classes.dex file to jar (using dex2jar tool), now I need to deobfuscate it according to mapping.txt. Are there any tools that can help me? Maybe Proguard itself?
UPD. I need a tool that can automatically rename all the classes, functions etc, so I dont have to do it manually. Thank you
If you have the mappings then you can get every class, field, and method name back.You can use java-asm (A bytecode modification library) to remap the class and member names. Most of the work will be updating member descriptors.
I made one for myself and it took about a day to get it working reliably.
Edit: It's not perfect, but it's pretty darn close. Link.
Give it a proguarded jar file, the mappings and it'll rename all it can find. There's a few issues but for just looking at the source it should do the job. Just dump the jar it makes (FileName + "-Re.jar") into a decompiler such as Luyten/JD-GUI. Also the file inputs are only changed via the buttons. The textboxes are just for visual-feedback/confirmation.
Edit 2: Fixed NPE occurring when class entry in mappings doesn't exist in the given jar.
Edit 3: Fixed a lot of issues resulting in methods not being renamed.
Edit 4: Uploaded compiled jar to github (releases, in the zip file it's in the mapping util), added support for multiple mapping types.
I suggest to use proguard tool as suggested in above answer along with Enigma. The solution for your problem can't be very straightforward and you need to use a combination of available tools to resolve it.

Is there an easy way to copy libraries between projects on netbeans?

Often I find that I have a set of JAR files configured as libraries in one Netbeans project and I need the same (or a subset of) those JAR files in another project.
All of the ways I've found to do this so far are very long winded and painful so I am hoping I've missed something simple.
Google found a 6 year old feature request and nothing else really useful: https://netbeans.org/bugzilla/show_bug.cgi?id=118325
At the moment the ways I can think of are:
Create a "Library" for the set of JARS and then use that library in each project. This is probably the neatest way to do this but still involves a lot of steps and seems like massive overkill just to copy a few JAR file references!
Go to source project, right click JAR, select edit. See the library classpath in the popup window. Close window, go to the destination, select add JAR. Can't cut and paste in path so have to hope you remembered it right. Browse to JAR file, add it - repeat for any other JARs that are not in the same folder.
Frustratingly the tooltip that comes up when you hover over the library contains exactly the path you need to add the jar - but there is no way I can find to copy the contents of that tooltip!
This is all made much more painful by the fact that all the windows are modal, you can't cut and paste the path out because it doesn't actually copy as a real path, etc.
All suggestions welcome as this is becoming a frequent annoyance at the moment!
I just found this old question while searching for an answer to the same problem. I Couldn't find any other useful results so I copied the libraries in the nbproject/project.properties file to the same place in my new project.
The only references I found in the whole directory were like this
file.reference.commons-lang3-3.8.1.jar=D:\\Documents\\javaLibraries\\commons-lang\\commons-lang3-3.8.1\\commons-lang3-3.8.1.jar
file.reference. [.....many more]
includes=**
jar.compress=false
javac.classpath=\
${file.reference.commons-lang3-3.8.1.jar}:\
[....many more]:\
This part was just in between what I wanted, so I copied the whole thing but it was the same in the new project.
includes=**
jar.compress=false
So far it's all working fine. You could back up the project.properties file but I already had one nightly.
edit:
You can also copy over the javadoc and sources by copying the lines like
javadoc.reference.commons-lang3-3.8.1.jar=D:\\Documents[more path....]javadoc.jar
source.reference.commons-lang3-3.8.1.jar=D:\\Documents[more path....]sources.jar

How to detect duplicate JARs in the classpath?

Does anyone have code to detect duplicate JARs in the classpath?
Background: When there are two versions of the same JAR in the classpath, really strange things can happen. This can even happen when using tools like Maven: Change a dependency and build the WAR without cleaning first. Since target/webapp/WEB-INF/lib wasn't cleaned, the dependency will be in there twice.
Is there a safety-net for this?
JBoss Tattletale might help you with this.
It's a free tool which scans the JAR files used by your project and gives you a report about them.
Amongst its feature are:
Spot if a class is located in multiple JAR files
Spot if the same JAR file is located in multiple locations
Find similar JAR files that have different version numbers
There is a Maven plugin to do just that: maven-duplicate-finder-plugin
[EDIT] If you want to do it in unit tests yourself, use
getClass().getClassLoader().getResources( "com/pany/package/Java.class" )
If this returns more than one URL, you have duplicates on the classpath.
The drawback is that this only works for cases where you had conflicts in the past. On the positive side, it's just a few lines of code (or one line when you write a helper method) and it works on your build server.
System.getProperty("java.class.path"), split it, sort it, look at it with the human eye
:-).
It will not include the classpath derived from manifests inside other jars thou :-(.
Or use http://www.jboss.org/tattletale as one of the posters suggested.
I think the simplest way is to simply trash the target directory first. Hopefully copying all the .jar files in isn't going to be time-consuming.
Otherwise you're going to have to somehow compare sizable files (whether directly, via computed checksum or similar). Which doesn't sound very nice at all.
You can write a simple script to compare the md5 sum of every jar file to every other jar file and deleting duplicates along the way.

Categories

Resources