Changing java file - java

Can I change a java file, when I'm running it on JVM, using this file?
For example:
I run abc.java. In this program I've a textarea and I want to paste text from the text area into abc.java and save the changes. Is it possible?

3 steps:
modify your .java file just like you would modify a text file.
compile that .java file from within your already running JVM using javax.tools compilation tools
Instantiate your freshly compiled class using the ClassLoader.

No. Maybe what you want is dynamic define classes at runtime. If so, you could choose dynamic language like Groovy.

The JVM runs class files (.class) and not Java (.java) files. To "convert" a *.java file to a *.class file it needs to be compiled firs.
So change a *.java File will not infuent the JVM in any way, because it is compleatly not interessed in.
Only because of a totality answer
You can write a programm (.java) that when it is compiled an run (.class) change its own source file (.java) (for example changing a string by user input) compiles it (.class), and restart it selfe with its new compiled form. But this is defently not what you want! One would write this kind of software only to prove that this insane idea would work.
Anyway, what you need is a way to store data, in a file or a database. (Have a look at the java.io package for file handling, and for an tutorial).

(new answer for updated question)
"In this program I've a textarea and I want to paste text from the text area into abc.java and save the changes."
This seems like a strange loop, self-modifying java code?
Then yes, the tools nrobey describes would be the way to go, though I agree with Ralph that it might be a bad idea.

Related

How to make VS Code recognize .class file?

I'm trying to use VS Code with java, but I have an issue.
As a student, my teacher gives me a file named Clavier.class.
I have already wrote a programm with another IDE, and when I execute it in the command of Windows, it works without any problem. But, when I put this programm in VS Code, it doesn't recognize Clavier.saisirInt, which is a static method in Clavier.class. I guess that VS Code can't use a class which is in a File.class, but is there a way to make it works ?
Here is an image of my workspace and you can see the error at the bottom
I assume your lib/ folder is placed onto the execution classpath. If this is the case, that's where compiled class files should go, not next to your sources
Otherwise, you'll need to inspect the java command that actually runs your code to see what/if the -cp argument is set to, or include those class files similar to how the documentation shows for JAR files (which are just zipped packages of class files, and honestly what the teacher should provide instead)
By default, VS Code will reference all JAR files in workspace's lib directory using the glob pattern lib/**/*.jar
And so, you need to adjust this to include lib/*.class after moving your files there

program doesnt' recognize a .class file

it's my first time working with a .class file in java, I saw that a.class file is just a compiled java code, so I figured out I should add this class to my package and it should work like a regular java class file but it didn't, I'm working with Netbeans by the way.
the problem is that I cant use this class, the main class doesn't recognize it, asking me if I want to create a Rational class to fix the error
I am not entirely sure what you want to do with the class file, but my assumption is that you got it already in this form and want to use some of it's methods.
So if this is correct you should put the file in the folder that netbeans uses to store .class files
(in my case build/classes/java/main/Testing):
IDEs almost always store .class files in a separate directory so that you don't have to deal with them, because a .class file is compiled java code and in 99% of the cases you won't touch the .class file but you will change the .java file and compile it to a class file.
Also be careful if your .class file is a part of a package you should replicate the structure of the package like answered here.

How do I edit the actual code of a java .class file?

I have a jar that I want to take out a class file and add a few lines of code to it. I got class editor, but you can't actually change any code, you can change constants and that is all. I have a program that you can read the source code from a .class but you can't change anything on it. Is there a program or eclipse plugin that you can read and EDIT the source code from a .class file?
Have a look at decompilers. They'll transform the java-bytecode back to source code. One example of such a compiler would be JD: http://jd.benow.ca/
After editing the source code you would have to compile the code again and pack it in the respective .jar file.
If your file is an old enough version, you can use jasper to disassemble it into bytecode, edit the bytecode and reassemble it using jasmin. Unfortunately these tools have not been updated in some time.
They do not produce Java code; you'll have to learn Java bytecode. But it is more reliable than the so-called "decompiler" methods.
As Java .class files are in byte-code format, you cannot modify them the way you would edit a .java source file. Bytecode is a low level language closer to the machine language rather than Java itself.
If you need to modify the source, one option is to use a decompiler, e.g. JAD (Java Decompiler), to get a source file and then change it and recompile to .class using javac. Make sure you figure out which version of Java language (1.4, 5, 6, 7, 8) has been used for the original jar file.
What you need for this is a java de-compiler. This will take the bytecode out of your class file and convert it back to its source. From there you will need to recompile the .java files that the de-compiler produces.
Here are some java de-compilers that I have seen:
http://dcompiler.sourceforge.net/
http://jd.benow.ca

jar file with source code only

Suggest I have a HelloWorld.jar file, this file contains only HelloWorld.java , which is the source code of the application.
Will it be possible to run this jar file and execute the application, even though I don't have HelloWorld.class?
Yes, it can be done: See the javax.tools api. It is not easy, but it can be done....
You will likely be better off with a script that unjars the file, compiles it, and runs it.
Directly? No. java accepts only classfiles. To use source it must be compiled with javac. Nothing keeps you or a utility from compiling the source files to class files and using those, however.

how can I edit a Java application using .CLASS files

I've been trying to edit the .CLASS files inside this program using .jar containers, and a .bat file to launch it.
I'm not familiar with Java, and I've tried Java decompilers but they don't let me edit the source, only copy it. I don't seem to have a program that can actually correctly save and encode it.
What's the best option here to easily edit this Java program's .CLASS files?
Edit: To everyone who's enjoying downrepping this, the program is open source and permits modifications. So thank you for all the blame flinging about it being illegal.
You could try decompiling it with Fernflower, editing the code, then compiling it back into a classfile with the normal javac.
There is no real "easy" way. You could edit the bytecode...but that is a bit more involved. You can use JD but, that as you said, is only so useful.
Use java de-compliler to get source code from byte code, modify as per you want save in different location(like copy source code with your modifications) then compile & run.

Categories

Resources