0

Sorry if the title doesn't say what I'm trying to do very accurately but I wasn't really sure how to describe it in one sentence. Basically I have a jar file with a java file inside it, and I want to edit the java file. I used 7zip to do so and it worked, letting me edit the code in the java file. I saved it and my new code shows up when I open the java file in netbeans. However, the new code isn't actually in effect, and my program is still using the old code. I don't know if this matters, but the java file is named Templates.java, located in a package called net.sf.dynamicreports.examples in a jar named dynamicreports-examples-5.0.0-sources.jar.

Thanks in advance for any help anyone can offer.

Edit: tried importing the jar into a new project and editing from there before putting it back into the original project, but had no luck (check comment chain for further details). Still have no idea how to fix this and would greatly appreciate any help

Evan
  • 193
  • 9

1 Answers1

1

Your jar contains .class files and .java files. When you create the jar, the .java files are compiled and .class files are created from them. You should have a Templates.class file that corresponds to Templates.java. When you edited Templates.java, Templates.class was never recompiled. This class file is the one your program uses. The appropriate way to change a jar file is unfortunately to regenerate the entire jar.

So, what you should do is open the code you used to create the jar in the first place, make your java changes there, and create a new jar. You will need to do this even with the smallest changes.

I didn't make the jar myself unfortunately, I got it from somewhere else so I don't have the code used to make it, just the jar itself. What should I do?

Edit: I saw your comment that you did not make the jar in the first place. This isn't too big a problem since you do have the java source code. What you need to do is copy all the .java files in the jar and make a new jar with them. Or, simply use Eclipse or whatever your favorite java editor is to compile your Templates class, and copy the new Templates.class file it creates into the appropriate place in the old jar, overwriting the old one.

Where would I find the Templates.class file?

Edit 2: You find the class file in the bin folder of your project if you are using Eclipse. See this question: Find the .class file compiled by Eclipse or if you are using a different editor/compiler, search google for where it stores its .class files.

MMAdams
  • 1,508
  • 15
  • 29
  • I didn't make the jar myself unfortunately, I got it from somewhere else so I don't have the code used to make it, just the jar itself. What should I do? – Evan Jun 23 '17 at 16:22
  • Where would I find the Templates.class file? – Evan Jun 23 '17 at 17:06
  • I found the class folder but it only has .class files for the java file itself rather than any .class files for my jars. Am I looking in the wrong place or did I do something wrong? I'm using netbeans by the way – Evan Jun 23 '17 at 17:42
  • Where did you edit your Templates.java file? If you did it in anything other than notepad or some kind of text editor, it should have made you a .class file when it compiled it. I don't know netbeans and I'm sorry I can't be more helpful other than to tell you to just look for it. – MMAdams Jun 23 '17 at 18:31
  • When I used 7zip I was able to go into the jar file and found a notepad of the Templates.java file. Is it a problem that it was done in notepad? – Evan Jun 23 '17 at 18:41
  • Notepad doesn't make a new .class file for you. That's why I was saying the best thing to do is copy all the java files into netbeans and create a new .jar so new class files are created. – MMAdams Jun 23 '17 at 18:42
  • There's a lot of java files in the jar though, like literally 100 at least. Is there a way I can copy them all at once or do I have to individually create a java file for each and then pack them all into a jar? – Evan Jun 23 '17 at 19:12
  • there should be a way to import an entire jar into netbeans if it contains the source files, which yours does. Import it as a project, edit your one file, and export it as a jar again. – MMAdams Jun 23 '17 at 19:20
  • I did that and got a message saying "No netbeans projects added". I went to my netbeans folder and saw a new folder named "META-INF" that had a 1 KB file named MANIFEST.MF inside it – Evan Jun 23 '17 at 19:37
  • I'm sorry I don't know netbeans, maybe you should try looking for how to import a new netbeans project from a jar with source code in it. – MMAdams Jun 23 '17 at 19:39
  • Okay, I imported the jar into netbeans into a new project, edited the java file I wanted to edit, cleaned and built the project, made it into a jar, then replaced the old jar on my previous project with the one I just made, but my project has no difference in execution. What did I do wrong? – Evan Jun 23 '17 at 20:04
  • Honestly, that is exactly what I thought would fix the problem. Maybe something else is wrong? Without more information I'm afraid I can help you no further, perhaps try reaching out to the person you got the original code from and see if they have had similar issues. Good luck and have a nice weekend. – MMAdams Jun 23 '17 at 20:07
  • Damn that sucks. Thanks for your help anyway, I really appreciate the effort. Hopefully I'll find a way to get it fixed. – Evan Jun 23 '17 at 20:08
  • Hey I think I might know what the problem is. Before I imported the jar into netbeans I edited the java file I wanted to edit using 7zip, which had me edit it in notepad. I know you said that that doesn't update the class file, but does that still hold true if I import the jar into a new project? If so, how can I edit it so that the class file updates? – Evan Jun 27 '17 at 19:40
  • the class file should update automatically when you export your project as a jar. – MMAdams Jun 27 '17 at 19:46
  • How exactly do I export as a jar? Usually i follow the steps [here](https://stackoverflow.com/questions/9681876/how-to-create-a-jar-file-in-netbeans) but it doesn't seem to be working for this project, and the "export" dropdown box under file only lets me export to zip, which doesn't seem to make a jar. – Evan Jun 27 '17 at 19:56
  • it sounds like your project's not set up right. Try creating a new project, copying into it all the java files from the jar. Then make your change to Templates.java and then export a new jar. – MMAdams Jun 27 '17 at 20:00
  • So I need to put in the files of the jar rather than the jar itself? Where can I find the files? When I look for them all I find are the jars, and nothing happens when I try opening them – Evan Jun 27 '17 at 20:06
  • The file that you edited in Notepad in the first place. That one and any others that it needs to be compiled without errors. – MMAdams Jun 27 '17 at 20:13
  • I went into 7zip, took the folder for all the java files, and copied it to somewhere else, but it won't let me import that as a netbeans project- it just tries importing the folder that i pasted it into. – Evan Jun 27 '17 at 20:20