0

I am developing a project in java in which ,after running main file , some java files get altered and if i run that file again during the same execution the output does not show the changes done in the java file

For example there are 2 files. Main.java and file1.java

main.java

public static void main(string[] argv)
{

    file1 obj = new file1();
    obj.view();
        Scanner in = new Scanner(System.in);
        String x = in.nextLine();
    //before entering any value i manually updated the content of file1.java
    obj = new file1();
    obj.view();
}

file1.java (Before updation)

public class file1
{

    public void view()
    {

        system.out.println("This is test code!!");
    }


}

file1.java (After updation)

public class file1
{

    public void view()
    {

        system.out.println("That was done for Testing!!");
    }


}

Output :
This is test code!!

This is test code!!
  • Java is not interpreted at the language level, it is compiled to bytecode. Modifying source code means it has to be recompiled – maress May 15 '12 at 14:24
  • 1
    Modifying source code on the fly is an advanced technique. It doesn't sound like that is your intention and it certainly isn't required for the example that you've provided. What are you trying to accomplish? – GoZoner May 15 '12 at 14:28

3 Answers3

1

You have to recompile the code in order to see the changes.

What you can do is compile a string (after reading it from the file) with java and call methods of classes through reflection.

HERE is a step by step guide on how to compile a string programatically.

0

Updating a Java file will not impact the runtime instructions executed by the JVM.

When the compile a Java application the .java source code files are compiled into .class files containing byte code instructions which are in turn interpreted by the JVM. When the JVM requires a class it loads the appropriate .class file into memory via a special object known as a classloader.

Applying this to your example - when you first reference the class File1 the JVM will load the File1 class into memory. This in memory representation of the class will persist until either the classloader is destroyed or the JVM is restarted. No change to the file1.java class is visible to the JVM - firstly because the classloader wont reload the definition and secondly because the definition wont change until the file1.java class is recompiled.

To change an object's behaviour at runtime you can use the reflection API see here.

mmccomb
  • 13,516
  • 5
  • 39
  • 46
0

You don't need to compile source code to accomplish anything close to your example.

public class MyView 
{
   String the_string;
   public MyView   (String string) { the_string = string; }
   public void setString (String string) { the_string = string; }
   public void view () { system.out.println (the_string); } 
}

public static void main(string[] argv)
{

    MyView obj = new MyView("This is test code!!");
    obj.view();
        Scanner in = new Scanner(System.in);
        obj.setString (in.nextLine());
    obj.view();
} 
GoZoner
  • 67,920
  • 20
  • 95
  • 145