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
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.
Related
it's been a while since i've been working with java and especially with eclipse. My professor sent me a huge folder with many subfolders and subsubfolders, that mainly contain .class java files. Now I'm supposed to work on these files, but i just can't seem to figure out how to get all of them working. I found a few solutions for single class files, but i have a whole folder hirarchy here that i want to work on.
I hope you can help me - I read something about decompiling? How does that work?
Note that I have around 50 different files here that need to be accessible.
Thank you very much!
I suppose the class you are attending is not something like "CS 902 - Reverse Engineering", because if that was the case, you would know what to do with the .class files.
So, one of the following holds true:
Your professor has made a mistake, and instead of sending you the java files, he/she sent you the class files instead.
Your professor sent you the entire project, which contains both .java and .class files, and for some reason you have only managed to find the .class files, while the .java files are there, and you just haven't found them. Unfortunately, the convention in the java world is to store .class files in a subfolder under the project root, so if you copy the project folder, you are copying .class files together with everything else.
Your assignment is to write new code which makes use of classes and interfaces supplied by your professor, but your professor does not want you to have the source code of those files. In this case, you can still work with the .class files, because the public definitions contained therein are parseable by Eclipse and usable in your project, without any reverse engineering. So, what you need to do is to find a way to tell eclipse that these .class files form a "Class Library" which is supposed to be used by your project, and then go ahead and develop new .java files making use of the library. I don't remember how this is done in Eclipse, but you should be able to find it out by yourself, or look it up, or perhaps someone else might post a how-to answer. However, at this point we do not even know whether this is in fact what you need to do.
You can use a java decompiler like JD-GUI, you can find it at http://jd.benow.ca/ . This is a very handy tool to have when you want to view a decompiled version of your jar. There are plugins available for eclipse and intellij as well.
I was trying to create a .jar from a .java file for a school project but now I can't read my .java file. The code I had written was overwritten somehow and I need to know how to (if possible) revert my .jar file back to the code I had written. Thanks in advance because this is a time sensitive matter.
You could try "decompiling" your JAR file using any JAR decompiler, such as http://www.javadecompilers.com/.
You won't get exactly the source code you originally wrote, but the decompiler will do it's best to map the .class files embedded in the JAR back to Java source code.
If you are using Eclipse you can right click on your project and check in history for getting old code
Can someone please enlighten me how to organize files in my Project?
I work with the command line. I don't want to bother with IDE Tools for now. My tiny program consists of just one .java file, but uses a bunch of 20 libs or so.
This is what I did. In the first line of my .java I wrote package com.mycompany.app.module; The .java and later the .class file are located in
/home/sweethome/Projects/Proj1/com/mycompany/app/module
Next I wanted to create a jar to put package that would include
com/mycompany/app/module/LogonToken.class
Is this ok so far? Here comes my real question: where do I put the 20 libs this class needs to run?
com/mycompany/app/module/libs
The clean way to handle external dependencies is to use a build system like Maven, and declare them in a pom.xml file. This does not require an IDE.
At the very first moment, this seems to require some extra effort, but will actually amortise very fast.
Does your question is build jar file with 20 libs and class? If it's true, here is answer of your question
Creating a JAR File
Here is the answer to my own question: http://www.sergiy.ca/how-to-compile-and-launch-java-code-from-command-line/
Fairly standard java project consists of three top level folders:
/bin - empty folder that will contain compiled .class files
/lib - contains third party .jar files
/src - contains .java source files
I have a Java JAR file that was developed to run in the browser. It works fine. There doesn't appear to be any sort of build file associated with the source code I've been given and I'm attempting to create a project and/or build script.
The source code references some third party code. What I've done is unzipped the JAR file into a folder that has this structure:
\App\src\com
\App\src\META-INF
\App\src\applet
The "com" folder contains subfolders which contain the .CLASS files for the third party libraries. "applet" contains the .JAVA source code files (one folder, maybe 15 files).
The code appears to be written in 1.4 syntax (I manually attempted to compile some of the .JAVA files and it complained that some things were deprecated and/or required the older version).
I created a Java Project in Eclipse and pointed it to the \App folder. Then I went into the Properties for the project and went to Libraries and and clicked on "Add Class Folder" and added the \App\src\com folder. Then I went to "Order and Export" and moved this to the top. I also set the Java Compiler version to 1.4.
I am getting a large number of compiler errors, all of which seem to stem from the fact that it says it can't resolve an import. This import is the set of .CLASS files contained in the "com" subfolders. The namespace matches the directory structure.
This seems like it should be really straightforward, but I've tried various things and don't seem to be making any progress. Based on what I've read I'm sure it has something to do with the pathing but I'm at a loss at this point on how to fix it.
(I'm sure it goes without saying that I'm not a Java dev.)
Any thoughts or ideas as to where I'm going wrong?
Can you please try to right click on Eclipse's Navigator. Select Import>General>archive file> (Jar file that you want to import).
You should be getting the jar file correctly imported at this point. Do tell me if you face any problems.
I have made a small program in Java that displays its .java source with a gui. It does not use FileChooser to do this. I am reading the .java sources with the aid of following statements
String resName = "/dev/classes/"+name+".java"
Scanner s = new Scanner(FilePrinter.class.getResourceAsStream(resName));
where name is the name of the .java file i.e. if the file is MyProg.java then name==Myprog. Of course my program is inside the dev.classes package
The thing is that when I export my project to JAR and include source files this works because source files reside inside the /dev/classes/ directory.
However, I haven't yet discovered a way to make my program run in Eclipse or from the command line without giving me exception.
And of course when someone tries to add those source files to be used automatically as resource files the process fails.
Can I somehow use the same code both when running from Eclipse and from the JAR? It must be something very trivial but because I am not Java expert I cannot see.
I found how to do it. Actually you either have to use Ant or Maven. However, this can be done in Eclipse as well as follows:
On the Eclipse Project Properties>Java Build Path you can choose on the bottom Default Output folder: <your_project_name>/src.
This causes class files be compiled in the same directory as the .java files and finally does what I wanted.
Thanks to #AndrewThompson for suggesting to try this