-4

I want to create a java program that generates another java class in the same project. For example in the class Dragon.java, i want to write java code that creates another java class called fire.java. I do not want to use any GUI from eclipse, just pure code that generates another class from the execution of written programming in java.

I have tried making objects of a non existent class in hopes of the program automatically producing a class with that name.

Again, it doesn't have to be just a java class, is there a way to make other forms of files also? for example fol.flow, or of different names.

blurfus
  • 13,485
  • 8
  • 55
  • 61
Reddi nav
  • 19
  • 1
  • 6
  • Since a file like `fire.java` is just a text file, it seems you're simply asking how to write a text file from Java. Given that you can find a gazillion examples of how to do that online, I'm confused about what you're really asking. Please **edit** the question and clarify it. – Andreas Jun 26 '19 at 18:35
  • 4
    What is your actual goal? This sounds like an XY problem. – xtratic Jun 26 '19 at 18:44
  • Please note that creating the `.java` file alone won’t permit you to use the generated Java code from the program that has generated the file. For that you would need the compiler interface (search for it). – Ole V.V. Jun 27 '19 at 08:09

2 Answers2

1

Creating a new Java file is easy. You can use any FileWriter technique. But what need to be taken care of is that new Java file is valid java file and can be compiled to class file.

This link has working example of doing the same.

import java.io.*;
import java.util.*;
import java.lang.reflect.*;

public class MakeTodayClass {
  Date today = new Date();
  String todayMillis = Long.toString(today.getTime());
  String todayClass = "z_" + todayMillis;
  String todaySource = todayClass + ".java";

  public static void main (String args[]){
    MakeTodayClass mtc = new MakeTodayClass();
    mtc.createIt();
    if (mtc.compileIt()) {
       System.out.println("Running " + mtc.todayClass + ":\n\n");
       mtc.runIt();
       }
    else
       System.out.println(mtc.todaySource + " is bad.");
    }

  public void createIt() {
    try {
      FileWriter aWriter = new FileWriter(todaySource, true);
      aWriter.write("public class "+ todayClass + "{");
      aWriter.write(" public void doit() {");
      aWriter.write(" System.out.println(\""+todayMillis+"\");");
      aWriter.write(" }}\n");
      aWriter.flush();      
      aWriter.close();
      }
    catch(Exception e){
      e.printStackTrace();
      }
    }

  public boolean compileIt() {
    String [] source = { new String(todaySource)};
    ByteArrayOutputStream baos= new ByteArrayOutputStream();

    new sun.tools.javac.Main(baos,source[0]).compile(source);
    // if using JDK >= 1.3 then use
    //   public static int com.sun.tools.javac.Main.compile(source);    
    return (baos.toString().indexOf("error")==-1);
    }

  public void runIt() {
    try {
      Class params[] = {};
      Object paramsObj[] = {};
      Class thisClass = Class.forName(todayClass);
      Object iClass = thisClass.newInstance();
      Method thisMethod = thisClass.getDeclaredMethod("doit", params);
      thisMethod.invoke(iClass, paramsObj);
      }
    catch (Exception e) {
      e.printStackTrace();
      }
    }
}

Avinash Gupta
  • 208
  • 5
  • 18
0

At first I thought you wanted code generation, but you simply want to write to files or create them?

The simplest code to create file and write to it:

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;

public class Testing {
    public static void main(String[] args) throws IOException {
        Files.writeString(Paths.get("D://output.txt"), "some text to write", StandardOpenOption.CREATE);
    }
}

It uses only java standard classes, you don't need any libraries or anything external. Just make sure to write to the valid path, where you have access.

If you want to generate files with java code, you can just do it with the method above, but creating the String with code content is really hard, there are libraries for it and they are not easy to use for beginners. For example javapoet. I personally used javaparser, it has a lot of other possibilities besides generating code.

Shadov
  • 5,421
  • 2
  • 19
  • 38