patternjavaMinor
A program ran in a program
Viewed 0 times
ranprogramstackoverflow
Problem
I was feeling bored with nothing to do (well, sort of) and decided to write a program that runs a program in Java. What it pretty much does:
The code is below:
```
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
/**
* Represents some Java source code.
*
*
* This source code then can be ran via the {@link #run()} method, or can be
* just used as a storage for source code.
*
*/
public class JavaCode {
private static final String JAVA = ".java";
private static final String CLASS = ".class";
private static final String COMPILE = "javac ";
private static final String RUN = "java ";
private static final String IO_ERROR_MESSAGE = "An I/O error has occured.";
private final String code;
private final File codeFile;
private final String className;
private boolean isCompiled = false;
/**
* Creates a new JavaCode object.
*
* @param code
* The source code.
* @param className
* The name of the public class
*/
public JavaCode(String code, String className) {
this.code = code;
this.className = className;
this.codeFile = new File(className + JAVA);
try {
// Delete any previous file with same name
this.codeFile.delete();
this.codeFile.createNewFile();
writeCodeToFile();
} catch (IOException e) {
System.err.println(IO_ERROR_MESSAGE);
e.printStackTrace();
}
codeFile.deleteOnExit();
new File(className + CLASS).deleteOnExit();
}
private void writeCodeToFile() throws IO
- Creates a new file with the name
classname + ".java"
- Write the code into that file
- if it is not already compiled, run
javac filename.javaon the command line
- run
java filenameon the command line
The code is below:
```
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
/**
* Represents some Java source code.
*
*
* This source code then can be ran via the {@link #run()} method, or can be
* just used as a storage for source code.
*
*/
public class JavaCode {
private static final String JAVA = ".java";
private static final String CLASS = ".class";
private static final String COMPILE = "javac ";
private static final String RUN = "java ";
private static final String IO_ERROR_MESSAGE = "An I/O error has occured.";
private final String code;
private final File codeFile;
private final String className;
private boolean isCompiled = false;
/**
* Creates a new JavaCode object.
*
* @param code
* The source code.
* @param className
* The name of the public class
*/
public JavaCode(String code, String className) {
this.code = code;
this.className = className;
this.codeFile = new File(className + JAVA);
try {
// Delete any previous file with same name
this.codeFile.delete();
this.codeFile.createNewFile();
writeCodeToFile();
} catch (IOException e) {
System.err.println(IO_ERROR_MESSAGE);
e.printStackTrace();
}
codeFile.deleteOnExit();
new File(className + CLASS).deleteOnExit();
}
private void writeCodeToFile() throws IO
Solution
Here:
This does not actually delete all the files. Consider this example code:
The program will delete the
codeFile.deleteOnExit();
new File(className + CLASS).deleteOnExit();This does not actually delete all the files. Consider this example code:
public class Foo {
public static void main(String[] args) {
Bar.sayHi();
}
}
class Bar {
void sayHi() {
System.out.println("hi");
}
}The program will delete the
.java file, but will not delete the Bar.class file. Leaving a mess was not what I intended.Code Snippets
codeFile.deleteOnExit();
new File(className + CLASS).deleteOnExit();public class Foo {
public static void main(String[] args) {
Bar.sayHi();
}
}
class Bar {
void sayHi() {
System.out.println("hi");
}
}Context
StackExchange Code Review Q#110076, answer score: 5
Revisions (0)
No revisions yet.