Hello there forum!
I've been dealing with something piece of cake. Right.
Piece of difficult cake.
So here is my personal project I've been working on, and I've already been receiving help from some of you (thanks!!! ) helping me get out mindfarts.
One of the objectives of this app is to be able to create a .java then compile it, and execute the file in runtime.
So far, my program is completely functional but I am facing a major challange.
I am only able to make it partially work when I give the program a predefined location. I decided to be able to make use a static variable Location, and some other variables sent as parameters to compile and execute.
What I've been doing all along was Runtime.getRuntime().exec(command); to access the cmd and compile or execute the desired file, and I haven't been able to get to where I want. I started reading about the ProcessBuilder class, and I don't get it.
Been close to 4 to 5 hours trying to figure this one out. It has become a royal pain in the butt.
Any suggestion or assistance would be greatly appreciated by this fellow here.
import java.io.*; import java.util.*; public class Mercury { static private Vector <String> SAVEDDIR = new Vector <String>(0, 1); static private String Path; static private String Code; static private String Name; static private String Location; private static String setLocation(){ String SubLocation = ""; /** ! ei. "C:\\Users\\Andrew\\Desktop//" */ return "C:\\" + SubLocation; } private static String nameCreator(){ Random R = new Random(); int i; String name = ""; char lower; for(int k = 0; k < 7; k++){ i = R.nextInt(26)+65; name = name + (char)i; name = name.toLowerCase(); } return name; } private static String codeGenerator(){ String Body = ""; /** ! */ return ("import java.io.*;\nimport java.util.*;\n\npublic class " + Name + " { \n" + Body + "\n}"); } private static void setCreationParameters(){ Name = nameCreator(); Location = setLocation(); Path = Location + Name + ".java"; Code = codeGenerator(); } private static void createFile(String ParamPath, String ParamCode){ File file = new File(ParamPath); boolean blnCreated = false; try { blnCreated = file.createNewFile(); } catch(IOException ioe) { System.out.println("Error while creating a new empty file :" + ioe); } System.out.println("Was file " + file.getPath() + " created ? : " + blnCreated); boolean blnWritten = false; try { blnWritten = file.canWrite(); FileWriter writer = new FileWriter(file); writer.write(ParamCode); writer.flush(); writer.close(); } catch(IOException ioe2) { System.out.println("Error while writing in file :" + ioe2); } System.out.println("Was file " + file.getPath() + " written ? : " + blnWritten); SAVEDDIR.addElement(ParamPath); } private static void createFile() { createFile(Path,Code); } private static void printLines(String name, InputStream ins) throws Exception { String line = null; BufferedReader in = new BufferedReader( new InputStreamReader(ins) ); while ((line = in.readLine()) != null) { System.out.println(name + " " + line); } } private static void runProcess(String command) throws Exception { Process pro = Runtime.getRuntime().exec(command); printLines(command + " stdout:", pro.getInputStream()); printLines(command + " stderr:", pro.getErrorStream()); pro.waitFor(); System.out.println(command + " exitValue() " + pro.exitValue()); } private static void compileFile(){ try{ runProcess(new String[] { "cd " + Path + "; javac " + Name + ".java" } ); } catch (Exception e) {e.printStackTrace(); } } private static void executeFile(){ // Needs try{ runProcess(new String[] { "cd " + Path + "; java " + Name } ); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) { setCreationParameters(); createFile(); compileFile(); executeFile(); } }
So yeah. Long story short, how can I get this part to work. Should I change strategy?:
. . . private static void compileFile(){ try{ runProcess(new String[] { "cd " + Path + "; javac " + Name + ".java" } ); } catch (Exception e) {e.printStackTrace(); } } private static void executeFile(){ // Needs try{ runProcess(new String[] { "cd " + Path + "; java " + Name } ); } catch (Exception e) { e.printStackTrace(); } } . . .
If I only place one cmd, removing the "new String[] { ... }" decor it works only when the file is at the default context I selected in the command prompt. In the mean time, I am unable to compile program its says..
C:\Users\Andrew\Desktop>javac Mercury.java
Mercury.java:72: error: method runProcess in class Mercury cannot be applied to
given types;
try{ runProcess(new String[] { "cd " + Path + "; javac " + Name + ".java
" } ); } catch (Exception e) {e.printStackTrace(); }
^
required: String
found: String[]
reason: actual argument String[] cannot be converted to String by method invoc
ation conversion
Mercury.java:76: error: method runProcess in class Mercury cannot be applied to
given types;
try{ runProcess(new String[] { "cd " + Path + "; java " + Name }
); } catch (Exception e) { e.printStackTrace(); }
^
required: String
found: String[]
reason: actual argument String[] cannot be converted to String by method invoc
ation conversion
2 errors