I would like to know if there is a way to edit the codes in a class file? Because I dont seem to be able to compile a java file into a class file with the use of cmd as it will always detect errors . The Jar file that I am using already have its own existing class files and I would like to update one of the the .class file with a few lines of codes. But when I copied the codes from the class file and put it into a java file together with my added lines of codes, and then try compiling it using cmd, the cmd will generate error whenever I tried to compile it to generate the class file.
Does that mean that I will need to de-compile the whole jar file and make it into a java file and then recompile it into a class file then make it into a new jar file again,? In order to achieve what I am trying to do?
Right now I am using this JCIFS jar file and I would like to add in some lines of codes into one of the class file. I am refering to this "https://code.google.com/p/android-smb-streaming/" as a guide and I discovered that this person is able to customize / add a new class file with the same lines of codes from the existing class in the jar file together with his own added lines of codes.
May I know how do I achieve this? Thank you.
What you have to do here is a common situation: you found a bug/feature request for an open source project.
Instead of hacking it you could try to contact the author of the library, submit your contribution and wait until it gets a new release so it has your new cool feature.
On the other hand, you can use the exposed API to customize it for your needs, e.g., create a specific subclass that overrides the required method containing that few lines of code and use the API in a way that it uses your implementation.
If you want to go the hacky way, there's no need to manipulate the classfiles directly (it is far more complicated): the easy way is to download the project, read the docs how to set it up (so you can compile it with cmd w/o errors), add your patch, compile it, then you can update the used JAR file with the new class file (or files: note that there might be multiple files generated if you're using inner classes or lambdas). Just don't forget to mention that somewhere in the docs, because otherwise no one will ever know that the library is slightly modified...
Related
I want use java code to run some clojure files dynamically which are in some zip files.
If the clj.p1.core.clj is on the class path, it can runs correctly.
require.invoke(Clojure.read("clj.p1.core"));
How to make it dynamically?That is, put clj.p1.core.clj in the a1.zip (maybe some files), the java program could select the zip and then run it?
Probably, you should unzip those files first and then specify a *.clj file when invoking Compile class; take a look at its sources.
What would be much better in your case is to compile a Java class from Clojure sources first and then load that class in Java as well. Just add a specific step into your build process that cares of it. In that case, your Java code will look much simpler and wont' waste time on loading Clojure code dynamically.
Creating a Java file would be easy; just wrap Clojure sources with additional namespace with gen-class declaration. Move its output into your Java project or specify classpath properly. See gen-class page for more examples.
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.
I am using a Java external library, a .JAR file that contains a number of classes.
I Have two questions:
I have a problem when using classes in the .JAR file. The problem is when some variables is defined in the class itself, how can I access it? Does the class in the .JAR itself finds it automatically or I should call it?
I would like to know which is best to do: using an external library .JAR file or creating the classes and methods included in the .JAR file and include them in the project I am working on assuming that I have both the source code .JAVA files and the .JAR file of the classes I need to use?
Consider the code below, it is from an external project that I want to use in a current project, I have both .JAR and .JAVA files.
For example the code below has a variable named original_executer that is defined outside this method. If I call this method and give it the required string, will it do its function properly or an error will rise?
public boolean readSet(String setName){
testSet = testSetName;
OriginalLoader myLoader = new OriginalLoader();
original_executer = myLoader.loadTestClass(testSet);
original_obj = original_executer.newInstance();
if(original_obj==null){
System.out.println(" Can't instantiate original object");
return false;
}
return true
}
If you add the .jar to your classpath, you can use everything as if it was defined in your project.
If your .jar file is a external library it is best to keep the library in the .jar and use it from there. Whenever the library gets updated, you can just overwrite the libraries .jar.
given your jar is properly added in the classpath and you have used the necessary imports in your code you can use any class or variables with correct modifier of the jar...
best is to use the external library as jar..and to consume it through package dependency tool like Maven which will automatically download the latest version of jar for you. And then you can compile and run against the latest version
To access the variables defined in the class, you would need to use the getter methods that are supplied. Otherwise, you would need to employ Reflection to grab the values by doing something like
Class.getClass().getField("field_name").set(Class.getClass(), "value");
Although I'm not 100% sure on the validity of that, I'm sure it is something along those lines.
For the second question, I'm not quite sure what you're asking but you should always just add the .jar file to the classpath or if you want to modify the library, download the source code of it and put it into your workspace.
I have a jar file which includes seviral classes. In that jar there is a Confirmation.class file which i want to edit. I decompiled that class by usin JAD. Then i edit it with notepad++ and saved as .java file.
Now how can i create my new jar file with other classes?
Other files format is .class but mine is .java, is it problem?
If it is , how can i compile my .java class ? (when i use command javac Confirmation.java it gives errors and want other classes)
Thanks..
Do you have dependencies to other jars?
If this is the case, you will have to put them on the compiler classpath in advance.
In any case, just decompile all the package (I use jd-gui), change the class, compile, open the original jar as a zip, put the new class on it, and you are done.
Another approach is to create dummy classes for the missing dependencies...
Personally, I really discourage this approach of "reverse engineering" working with an already compiled package, but I understand situations where you don't have access to the original source code, but you need to fix something urgently (ex: working in a company, where your code comes from an external provider on the other side of the planet...)
You may try to copy & paste the code in Java IDE such as Eclipse, NetBeans, etc and ask the IRC to compile for u. Have a nice day :)
We have an java application in which the user can write/execute their own java code and use imports from compiled jars - i.e. they write it, and it is compiled and run by the application. They can also save this code (along with various other information that they are using) - currently this is saved to a human-readable xml file.
I want to be able to use those save xml files in an IDE (principally, Intellij), so that if the user changes things in their compiled jar in the IDE, these changes can also be picked up in the save xml file.
For example, if a save file used a class from the compiled jar, it may have the following import:
import com.company.project.package.subpackage.MyClass;
Let's say that class was moved, so the import was:
import com.company.project.package.subpackage.subsub.MyClass;
...this would change all the save xml files that used that class and import - just as the IDE would for all the other usages in the compiled project.
(This, and other examples, arises because the compiled jar is both constantly in development and in use using the aforementioned application.)
At the moment, if I were to add the save xml files to a sub-project in the IDE, the user can edit the save files manually, possibly taking advantage of 'find/replace' or 'search for usage in text' functions. This is better than nothing, but still a rather involved/complicated process. Also, there is no checking that the code in the save files are consistent with the code in the compiled project.
One approach that I am considering is a script or a test class that would unpack the save xml file, writing the java code to java files, and then try and compile (and possibly running/testing) those java files.
A further step would be to write a maven plugin (we use maven for our build cycle) or an ant script (ant still has its uses...) to do this, and possibly make this part of our build process - i.e. you cannot compile the project without ensuring all of the save xml files in its sub-project also compile.
Does this seem like a reasonable approach?
Are there alternative approaches that anyone could suggest?
..saving as a .java file is not the solution I'm looking for.
Save it as a Zip with 1 (or more) XML files as well as any source (in paths according to package) that is required. You could even include other files easily, a manifest, help files etc.
This has a number of advantages:
It allows source & include files to be a different encoding to the XML
It consolidates all the necessary parts of the project into one file, without any 'jumping though hoops' to make one format fit inside another.
It allows different compression levels as appropriate to the data (e.g. text/XML compresses well, whereas a serialized image does not).