0

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?

1
  • 2
    java file won't go into JVM, but java bytecode does. Commented Jan 22, 2011 at 15:06

4 Answers 4

3

3 steps:

  1. modify your .java file just like you would modify a text file.

  2. compile that .java file from within your already running JVM using javax.tools compilation tools

  3. Instantiate your freshly compiled class using the ClassLoader.

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

1

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).

Comments

0

(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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.