简体   繁体   中英

how to make java use the newest version of a file

I have this problem that i have a program that writes and creates a .java file and puts it in my package folder, after this it takes the information from the .java file and uses it in it self. (it creates a new class with a method that i then import). The problem is that if it wont work until i with eclipse update the "self created file". is there a way to make my main file update the "self created file".

Sorry if this is a duplicate. I just couldn't find it any where.

my code:

package dk.Nicolai.Bonde;
import java.io.*;

public class main {

public String outputString ="Math.sqrt(25)" ;
static String outputPath ="src/output.txt";
/**
 * @param args
 * @throws UnsupportedEncodingException 
 * @throws FileNotFoundException 
 */
public static void main(String[] args) throws IOException{
        new main().doit(args);
    }

public void doit(String[] args) throws IOException{
    PrintWriter writer = new PrintWriter("src/dk/Nicolai/Bonde/calculate.java", "UTF-8");
        writer.println("package dk.Nicolai.Bonde;");
        writer.println("public class calculate{");
        writer.println("public void calc(){");
        writer.println("System.out.println("+outputString+");");
        writer.println("}");    
        writer.println("}");
        writer.flush();
        writer.close();

    calculate calcObj = new calculate();
    calcObj.calc();

}
}

Your main mistake is that you expected that it's during runtime automagically compiled into a .class file after save (which a sane IDE such as Eclipse is doing automatically for you behind the scenes everytime you press Ctrl+S). This is thus not true. During runtime, you need to compile it yourself by JavaCompiler and then load by URLClassLoader . A concrete example is given in this related question&answer: How do I programmatically compile and instantiate a Java class?

You'll in the concrete example also notice that you can't do just a new calculate(); thereafter. The classpath won't be auto-refreshed during runtime or so. You'd need to do a Class#forName() , passing the FQN and the URLClassLoader . Eg

Calculate calculate = (Calculate) Class.forName("com.example.Calculate", true, classLoader);

Your other mistake is that you're relying on the disk file system's current working directory always being the Eclipse project's source root folder. This is not robust. This folder is not present at all when building and distributing the application. You should instead write the file to a fixed/absolute folder elsewhere outside the IDE project's structure. This is also covered in the aforelinked answer.

No, you cannot. You have to manually update resources in Eclipse. Although you can write a plugin for Eclipse which runs your file and update resources.

Eclipse uses directories and files to store its resources but is not direct representation of file system.

Your code could not work, because

  • calculate is required at compile time of main. You supply it at runtime.
  • calculate.java will not compiled, so even other techniques to dynamically load classes will not work

If you want to build classes at runtime, consider to use the reflexion API

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM