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.
Related
(Thank you #MarkRotteveel !)
I'm learning Java. I want to import class of another project. 'a' class('MySoup') in project A('recipe_nomodule') to project B('test')
However, there is no JRE System Library and it doesn't work. And I can't find posts about this problem
ㄴ 1. I created another project. (recipe_nomodule)
and a package(recipe_nomodule) and also a class(MySoup)
ㄴ 2. I created recipe.jar
ㄴ 3. Java Build Path - Libraries - Add External JARs... (in Properties)
ㄴ 4. result.
"MySoup cannot be resolved to a type"
"The type recipe_nomodule.MySoup is not accessible"
(23/01/2023 Thank you for the answers)
In the 'build path' settings of your new project (as per the screenshot you posted for item 3), add the other project in the 'project...' tab.
You don't need to make a jar, you don't need to add that jar via 'external libraries..'. You don't want to do that - you want the project dependency. That way, you can edit a file in either project, just save the file, and run, and see the updates, without having to go through the routine of making another jar. In debug mode, any change that doesn't affect signatures will even be instantly applied, no need to restart (only applies to long-running apps, of course).
Why doesn't your current approach work? Your question doesn't include enough information to be sure. A few options, based on the fact that key error message is 'not accessible':
You've explicitly told eclipse you don't want the package recipe_nomodule to be considered accessible by anything except the project that created it. I doubt you've done this.
It's set up as a module-based jar (with a module-info file), in which case, you'd have to export the package in the module-info.java file.
The class wasn't marked public at the time you made the jar file, even if it is now (this gets back to: Don't go via jar files, that way any updates won't propagate and that's very annoying).
More generally if you want to ship these concepts completely separately, you need to start answering questions about how you'd like your jar file to be distributed, you need to sign up with sonatype/mavencentral or some other entity that distributes open source, you need to learn maven, and more. Probably not worth getting into that, especially given that 'for fun' / 'for exercise' projects wouldn't be accepted.
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.
I have been searching for a long time on how to step through imported code classes in netbeans with no solution.
When debugging or stepping into the code, netbeans does not highlight and stop at the imported code classes being used. This is extremely inconvenient since the code I am working on is complex and stepping through is the only way to fully understand it.
I created a new project with existing sources. I have made sure that under window>debugging>sources I have added my working folders.
I also made sure the sources were added in the "runtime" classpath.
The compile classpath appears to be empty, I'm not sure whether that has something to do with it.
MORE INFO: In the debugging window while the program is paused, the main method profile from my imported class says it is reading from "Hidden source calls", but it is really just the normal imported code.
Why is this happening? Can anyone please help?
Folks,
One of the reasons you can get Hidden Source calls is adding in existing packages and java source code at the wrong directory level. You MUST add in the parent directory of the package not the child directory that represent the package name,
example if you have /home/torsi/java/src/torsipackage/com/main.java, you need to add in src, not torsipackage, you will now you made this mistake if you see listed in the top-level package name for your package.
Tom
Well I've not heard the term "Hidden source calls" before but I wonder if you're trying to step into third party code? If you want to step into compiled code it must have source associated I believe. I think it's possible to step into native code because they include the source somehow in the distribution for that exact reason. If you want to step into a library that's been developed by some other private entity you will likely not be afforded that luxury unless it's open source, and even then you may have to retrieve the source first.
Ultimately my point is that there is a difference between a .java file and a .class file. I believe it's possible to reverse engineer .class files but haven't done research on the topic.
If you want to step into code you didn't write odds are you'll need to change your code to work with the imported library or alternatively file a bug report with the developer.
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
I've downloaded the full AOSP source for my device, and was trying to import it inside eclipse to make some changes to the framework (basically it includes an alertDialog that shows when a button in the system bar is pressed and i want to edit it).
I've followed the guide on the AOSP Documentation, and also imported in my build path the android-common_intermediates/javalib.jar but i still get some errors, to be specific I get errors with some fields that should have been defined in one R.java file and I have no idea of how to find the proper R file, i even doubt it's there, maybe is it generated in a second moment or is it in another path and then my compiler script moves it to the right path?
Presuming you heed the warnings in the comments above and still want an answer to the question, I may be able to help. I frequently see similar problems, when I use eclipse to view Android source. There are several projects that are, apparently, not built in response to "make" but that are still included by the .classpath file. My solution is to make them by hand.
Once you've sourced build/envsetup.sh, the command "mm" will build the project in the directory you are in. I start eclipse, find the projects whose resource definitions are missing, cd into them one by one, and use "mm" to build them.
I believe that you will find that they are all projects in the "packages" directory.
So far, this has always worked, for me.