Minor Edits to Class File inside JAR - java

I have a custom legacy application built in Java that I need to make a very minor edit to. I need to change a reference to Year of Week 'YYYY' to Year of Day 'yyyy' in one class file.
I found out that vim can edit the class files within JARs which seemed the easiest solution, so using that to make the edit a copy of the JAR. However, when trying to run the new JAR I am hitting exceptions for the class I edited stating:
"Exception in thread "main" java.lang.ClassFormatError: Extra bytes at the end of class"
Looking at the file sizes, it looks like even though all I did was change 'YYYY' to 'yyyy' on one line, the file is 52 bytes larger than the original. I think vim is adding extra characters when it saves. I tried 'set nofixendol', 'set binary noeol', 'set binary noendofline' before saving since I saw people mentioning vim adding endoflines, but it hasn't helped.
It's such a minor change in a huge bundle of custom code, but I can't seem to actually make the change.
Previously tried to compile from source or from decompiling the JAR, but always failed due to dependency issues, no idea how it was originally compiled. Tried including the original JAR when compiling and it still didn't work. That's why I was so hopeful when I found vim could edit it.
Any ideas on what's going wrong with vim editing or other ways of making the minor change to this app would be appreciative. Losing my mind a bit here.

You should use a hex editor (if you're using Linux, hexedit should be available in command line) to edit binary files. Text editors usually adds something to the content or convert characters.
Oh, and if you want to make more complex changes JBE is pretty cool.

Related

IntelliJ keeps switching to UTF8 (I want to set CP-1252)

I have some projects which are encoded with Windows-1252/CP-1252 and I can't change the encoding. The problem is, no matter what I do, intelliJ will keep trying to read these files as UTF-8 unless I manually put every single file in the encoding list.
That requires a lot of time and effort, it's error-prone and it's not a solution at all. I have set the entire project and IDE encoding as CP-1252 but it keeps trying to read files as UTF-8 anyway.
I don't know what causes that. We are using Subversion to commit files and maven to compile (which uses UTF-8 to read files except for the super POM which uses CP-1252).
Any idea how to solve the problem? I gave a look at other posts but I found no real solution yet. I'm currently using the last IntelliJ version (2017.1.2)
I actually found out what was the problem. Maven project encoding was overriding Intellij configurations. I tried to edit the source encoding property before but it didn't work because I misspelled Cp1252. Now it seems to work.

My BlueJ source code got deleted

I was doing my school project today when suddenly my java BlueJ crashed and both my current file and my backup file got deleted.
One of my most important classes(SignIn.java) got deleted(only the source code).
While executing, it still works as it is linked but i can't find the source code to make changes or print it. There is a file with #BACKUP which might help me.
Is there anyway i can retrieve it? I spent almost 6 hours on this class and it took a lot of work.
As you can see in their manual at page 42 the #backup file is always saved in the same directory so your #backup file should be gone.
If you versioned it with svn or alike in the Team menu you can get Project History.
Otherwise.... you can use some file recovery programs.
The most popular can search for source codes in many languages.
The advantage of this approach is that you will recover comments meanwhile you won't by decompiling.
If your source code is truly gone (non-recoverable) you can try to use a de-compiler against the .class file. Jad and FernFlower are two that I've used and work pretty well.
https://github.com/fesh0r/fernflower
http://www.javadecompilers.com/jad

.class file isn't syncing with .java file when debugging

I might be using the wrong term when talking about "sync" but that's the best way to descrie it I think. I'm trying to debug a project that I've been working on for a couple of days now but for some reason the .java class and the .class file aren't syncing (.class file isn't updating to any of the changes that I make to the .java class that I'm editing). Let me start from the beginning I guess, when I do add break points to my .java file (sometimes the breakpoints are skipped) that I'm debugging and run the debugger when the breakpoint is reached the .class file is opened in instead of the .java file.
I searched around and found the solution to that with the help of some topics on here, which was "Edit source lookup". So the part about .class opening instead of .java file instead was solved. The main problem now is when I do edit the .java file with anything and run the debugger it completely ignores my changes. For example, if I implement a System.out.println("Test: 1") before a breakpoint or even after it's not printing to the console. Another example is if I comment out a line of code and run the debugger, the debugger still executes that line of code that I just commented out.
I searched for some solutions but they didn't help much. Some things I've tried were but did'n work:
Why isn't Eclipse updating the classes?
1.) Make sure the Build automatically is checked
2.) Cleaning the project
3.) Refreshing the project, F5
4.) Go to Wwindows->Preferences->General->Workspace and making sure the first 2 checkboxes are checked.
5.) Saving
6.) Restarting ecslipse
If anyone knows how to maybe solve my propblem I will be indebted to you. By the way I'm using java 1.7
What you describe indicates problems in you project definition.
Make sure that your project (main runnable class) specifies correct dependencies - that it does not specify dependencies on other jar files but includes dependencies from your related projects

Packaging external files with JavaFX deployment

I have a JavaFX app that reads in a configuration file. I'd like the config file to remain outside of the jar to facilitate modification without recompiling. Is there a way to set up netbeans to grab the config file and include it in the installer?
I've found the option to change to icon and that works fine but I haven't been able to discover how to tell it to also include specific external resources.
I've read the information posted here: http://docs.oracle.com/javafx/2/deployment/self-contained-packaging.htm but I'm still not seeing a way to accomplish this.
I don't think there's a way to do this (though I may be wrong). I needed something similar to this once, and the approach I took was
Package the file in the jar file
At program start-up time, check to see if the file exists in the expected location on the local drive
If it's there, read it, etc
Otherwise, read the contents from the jar and write them to the expected file
This solves the problem of "deploying outside the jar", and it also solves the problem of the user inadvertently deleting the file after deployment, etc.
The way I solved this issue (due to having many dependent .dll's and other type items) was to use netbeans to compile the jar, which has things like images/css/fxml etc. etc. and then use an Inno Script to actually compile and configure the installer, since inno makes it pretty straightforward to include extra resources. I have yet to find a way to do this properly within javafx itself.

How to debug Java code when it is out of sync?

One of the problems I'm running into is that my jar files and my source files don't match.
I'd like to:
Be Notified when when source and binary don't match (I think Visual Studio can do something like this...)
Set break points not by line, but by function. For example, set a break point at the entry of function foo().
I use eclipse mainly; so Solutions for eclipse would be most appreciated, but any IDE (or command line debugger) will do.
Thanks!
When developing just use and link to .class files that are saved by your IDE.
If your project starts to grow to a point where it's really useful to link to a .jar you're better off treating the jar's as separate projects.
Go to Project tab in Eclipse and then
click clean: Project->Clean…
click build automatically: Project->Build automatically
Use method breakpoints instead of line breakpoints. They can be set to halt on entering/exiting a method. You get them by double clicking the left editor side in a line containing a method declaration.
The debug information is limited to line numbers in the source file... I do agree that having a warning that the lines are wrong would be nice, but that would require more meta data in the jar than I think is available...
You might want to consider addressing the challenge with a change in your build process. This isn't exactly answering your question, but hopefully it will give you a strategy that will address the underlying problem.
When you generate the jar for deployment, also generate a jar with the binary AND source. For investigating the source code of a stack trace on the live server, set up a separate project in eclipse and have the binary+source jar be on the classpath. You may have to explicitly set the source code location back to the same jar (though I think Eclipse will just do this automatically).
Then you just have to add copying of the binary+source jar into the appropriate location in your workspace as you do your deployments (preferably with a deployment script).
If it's your server, you may want to even consider deploying the binary+source jar to the live server - that way you will always be able to get at the source.

Categories