tags:

views:

251

answers:

4

I've been asked to help figure out why a java application doesn't run as intended.

Edit: Thanks to everyone that answered, but it turned out that the problem was an altogether different one.

In fact, it has nothing to do with java, even though the program is written in java. I've posted another question on ServerFault here if anyone's still interested.


I don't yet know what "not as intended" means. It could be an exception, it could be a disappearing act, it could be the wrong result, or something entirely different.

My expertise is in C#, .NET, Win32, Windows, etc. but not in Java, so I'm guessing they're a bit desperate, and I said I would take a look.

What I'm wondering, is if on this machine, all I have is the application itself, presumably in .jar files, can I debug this?

I will be allowed to bring my own tools that can be installed, etc. so any tips you can give.

Note that I'm not asking if anyone can teach me all I need to know. They already know my expertise is not in java, but I've had some luck with long-shot debugging attempts for similar things before so I guess this is why I've been asked.

So basically:

  • Can it be done?
  • If yes, what would I need, as a minimum?

Assume I'm enough versed in the use of debuggers and tools, etc. that you don't need to tell me how to use that part, except for what I need to do in order to load the .jar files and hit "Run with debugger".


As an example, I've attempted to make StatSVN run the past few days, so I loaded up Eclipse, imported the contents of the .jar file, and when I try to coach it into running, it complains that there is no main class in the project.

I did get lots of files and "things" into the project, however, and although the java IL is unknown to me, it would be a start, as long as I could coach it into running.

If at all possible, of course.


Ok, I managed to get Eclipse to debug the StatSVN package, but as some of the answers + comments here shows, the information isn't enough to get a grip on what is happening.

What I had to do (and this is probably bleeding obvious to any Java developers out there; please think of me as a caveman prodding a car with a stick and managing to take it out of gear so that it rolls down a hill):

  • Edit the project settings, and add an external reference to the StatSVN.jar file (not trying to import the contents as I had tried before)
  • Add a dummy Main class, which simply called: net.sf.statsvn.Main.main(args2);, and I created a dummy array (whaddyaknow, same syntax as in C#, or is that the other way around? hmmm) with the parameters that would make statsvn run (except that it doesn't handle the log file I threw at it, but that's a different problem).
+1  A: 

Then I suggest your first course of action is to find out why there is no main class in the project and what they use to run it otherwise. You may have to make your own main class for making a small testrun of this .jar-file. You do seem to be on the right track though, by managing to open it up as a project in Eclipse. If there is a proper package structure you should probably start looking at the packages highest in the structure first to try to get an idea of how the library was built.

Stefan Thyberg
When browsing (StatSVN example here), I've found that the manifest refers to net.sf.statsvn.Main, and I found the folders (packages I assume?) net, sf, statsvn and inside there Main.class, but of course there is no source code, only the compiled files. Could I use the .jar file as a library, make a minor class myself that calls it and pretend to execute the program that way?
Lasse V. Karlsen
You should be able to invoke net.sf.statsvn.Main.main(args)
McDowell
I added a new Main file in the default package, with the main method in it, but "net" doesn't seem to be there, so I assume I've "included" or "referenced" the jar incorrectly. How would I add a reference to that file the right way?
Lasse V. Karlsen
I managed to be able to debug it from Eclipse, thanks!
Lasse V. Karlsen
+3  A: 

If your java program is compiled with debug support, it will be be much better because you'll be able to know the exact line you're debugging, and enter into the methods.

Let's assume this.

You can launch your program in remote debug mode by using (assuming the program is in a jar)

java -jar -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=[address]

Then you can use any debugger to connect to this application, for example Eclipse, if you have the source code.

You can also just run it in debug mode in Eclipse, if you got the source code.

EDIT : To find source code, you can also use a decompiler, like JD. Then your debugging will Eclipse will hopefully be more useful.

Valentin Rocher
That last sentence, does that mean I cannot use Eclipse unless I have the source code, either for running from Eclipse, or connecting to it from Eclipse?
Lasse V. Karlsen
No, it just mean that debugging from Eclipse would be quite useless if you do not have the source code. Eclipse is an IDE, so its utility in this case would be to see directly in the source code where you are in your program. If you don't have the source code, I would suggest using a less gigantic application to debug, but I don't really know Java debuggers that well.
Valentin Rocher
I managed to get it to debug the statsvn application, but as you say, without the source, the information I can see is somewhat useless. However, it shows me classes it runs into, methods it runs into, etc. so while it is "somewhat" useless, it isn't completely useless, so if this is all I can get, so be it. This is going to be a long-shot attempt for my part anyway.
Lasse V. Karlsen
You can drill into classes on your project classpath, open them (even though you don't have sources) and use the "Outline" view to set break-points or variable watches.
McDowell
I added a section about decompiling, it's also a method you could use to nearly have the source code.
Valentin Rocher
Oh man, if that wasn't the bomb then I don't know what is! I installed JDEclipse, restarted, and immediately I get source code, well, decompiled source code, I know, but again, this is way better than looking at class+method+linenumber!
Lasse V. Karlsen
I'm marking this as the accepted answer. In truth, all the answers here helped tremendously, but the edit with JD was basically what makes me believe I can at least figure out *some* parts of the problem. Thanks!
Lasse V. Karlsen
+1  A: 

Yes you can debug jar's in eclipse, netbeans, or whatever... BUT I strongly suggest you practice before your first big night at the opera. Start with a few simple java programs. Leaping straight into to a subversion server is/was probably a mistake.

How much do you know about this application you've been asked to debug? What kind of app is it?

If it's a standalone desktop (GUI) app, or a fat client, then you're (probably) in business.

If it's a J2EE server application then sorry, you're in way over your head. My advice is to call the client and tell them that you need to find a "consultant" with "specialised skills", and this might take a few days... The good news is there's plenty of them out of work at the moment ;-)

Is it a commercial product? Java bytecode can be obfiscated to protect copyright, which effectively defeats debuggers. As previously stated, this will be a lot easier if the app is compiled with debug symbols enabled.

Good luck. Cheers. Keith.

corlettk
It is a desktop client-side application, that's basically all I know. The night at the opera is tonight, so I guess I'll find out soon enough :) Any direct points like "1. start Eclipse, 2. load jar, 3. Hit Run" that you can give? Still struggling with point 2 there, it seems to make a difference whether I import to /src or not in terms of whether the Main.class file shows up at all or not.
Lasse V. Karlsen
Managed to get statsvn debuggable from Eclipse by just adding a dummy Main that chained into the main method in the statsvn classes. Need the source in order to see what is happening, but it's a step in the right direction.
Lasse V. Karlsen
+1  A: 

If you are debugging something then there is a bug that needs to be fixed which means you will have to edit the source, which is impossible if there is no source code.

If the Jar contains executable classes then java -jar will execute it if it's correctly configured. if not then you will have to modify the Manifest file in meta-inf to contain the executable class.

A Jar file is basically a zip file with a different extension, so you should be able to open up the file using 7-Zip and view the contents.

Also Try decompiling the source code usign a Java decompiler, however YMMV.

http://java.decompiler.free.fr/

Omar Kooheji