I am trying to compile some java codes from a .java file that i had saved to disk. Basically i am trying to build my own ide. I have a text area in which the java code is in and also have two buttons. What i need is when i click the button the .java file on my disk is compiled and any errors are shown in the textarea
For the second button when i click it(if compilation is successfull) for the .java that has been compiled to be run.
Does anyone know how to compile and run a java program from a program?
I hope someone can help or show me some codings on how this can be achieved.
Any amount of help is greatly appreciated
Thank You
Yours Sincerely
Richard West
11-30-2004, 03:46 PM
doredson
See
Runtime.exec(String[]);
and the Process class.
12-04-2004, 04:22 PM
freesoft_2000
hi everyone,
I need to clear something with you guys
let's say for example if my javac file is located in the file location
"C://abc/fd/javac.exe"
and the file i want to compile is located at "D://ac/d/lpc.java"
so when i compile the file i must writethe following on the command line
"C://abc/fd/javac D://ac/d/lpc.java"
Am i right or wrong. If i am wrong then what is the correct way to do it
Thank You
Yours Sincerely
Richard West
12-04-2004, 05:23 PM
doredson
What I have done is pass something like this (Each string is an argument to the programs main() method):
//remember to use double-backslash if you're on windows
String command = new String{"C:\\abc\\fd\\javac", "D:\\ac\\d\\lpc.java"};
Process p = Runtime.getRuntime.exec(command);
//wait for p to finish, and display any messages if return code is negative, etc...
hope that helps.
If no, you could try passing "cmd", "/c" as the first to items of the String array. That works,too.
12-10-2004, 04:33 PM
freesoft_2000
Hi everyone,
Doredson i tried what you said but it still does not work. I always get the exception null pointer even though it is not empty but the program compiles with no errors
Here is part of the code
Code:
public void compile ()
{
byte[] buffer1 = new byte[2048];
int len = 0;
String str9 = "C:ddk1.4/bin/javac";
String str10 = "D:gui.java";
String[] str11 = {str9, str10};
File File11 = new File("C://JIDE_Errors.TXT");
try
{
Runtime1 = Runtime.getRuntime();
//The below command command line is where the exception
//occurs saying it is a null pointer exception
Process1 = Runtime1.exec(str11);
InputStream in = new BufferedInputStream(Process1.getErrorStream());
FileOutputStream out = new FileOutputStream(File11);
I tried the way you showed me but it still does not work. Am i missing something. I really do not understand why there is a null pointer exception.
I hope someone can help me with this problem
Any help is greatly appreciated
Thank You
Yours Sincerely
Richard West
12-10-2004, 04:48 PM
doredson
Take a look at str9, str10. They don't have correct path information...I'll try pasting that into eclipse and doing something similar...
12-10-2004, 05:19 PM
doredson
Your method worked as expected once I fixed a couple of compile errors...
I can't try exactly what you are doing, because I'm typing on an iMac right now. However, I did modify str9 to be "/bin/echo" and str10 to be "Hello World!", and as expected, the output file containted "Hello World!" after running.
So, with that in mind, make sure the path to javac is correct(with correct path separator characters). Those strings are incorrect in what you posted. See comments in the code below.
Here is the code (after I changed it to a main() method that I could run):
Code:
public static void main(String[] args) {
byte[] buffer1 = new byte[2048];
int len = 0;
//This is missing a slash between C: and ddk1.4...should it be jdk4.1?
//String str9 = "C:ddk1.4/bin/javac";
//This worked on a Mac and Linux:
String str9 = "/bin/echo";
//Also missing a slash between D: and gui
//Original String str10 = "D:gui.java";
String str10 = "Hello World!";
String[] str11 = { str9, str10 };
//Again, you need a correct string here. Either change the // to \\ or use 1 forward slash.
//Original: File File11 = new File("C://JIDE_Errors.TXT");
File File11 = new File("myfile.txt");
try {
Runtime Runtime1 = Runtime.getRuntime();
Process Process1 = Runtime1.exec(str11);
BufferedInputStream in = new BufferedInputStream(Process1.getInputStream());
FileOutputStream out = new FileOutputStream(File11);
There is no exception thrown but the exit value of the process is 1 (meaning the program exited abnormally). I do not know what i am doing wrong. Am i running the compiled class the correct way. If not then what is the correct way of doing it. The program i trying to run has no errors when compiled. Basically what i need is a way to change my class path programatically.
I hope some someone can help me with this problem
Any help is greatly appreciated
Thank You
Yours Sincerely
Richard West
12-12-2004, 03:47 PM
doredson
try printing out the messages you get, as in the earlier example, send the output to System.out, or something.
12-13-2004, 02:17 AM
freesoft_2000
hi everyone,
I already did that but the compiled class is not run but the program exits abnormally and there are no exceptions. I am thinking it has something to do with the classpath but i am not sure if i am setting it correctly in the above code snippet. I also tried this( as an attempt to change the classpath)
Code:
String[] str12 ={Rstr, "-cp.;", "C:/JButtons"};
// Rstr is the location of my java intrepreter
// The value of Rstr is C:/j2sdk1.4.2_04/bin/java
// C:/JButtons is the location of the class i want to run but can't
// seem to although the class exists at that location
What i think is that the problem has to do with the setting of my arguments in the str12 array and i maybe my setting of the class path is wrong but i am very sure of setting the classpath correctly
programatically
I hope someone can help me with this problem
Any help is greatly appreciated
Thank You
Yours Sincerely
Richard West
12-13-2004, 07:40 AM
doredson
Separate the -cp and the actual value for the classpath:
"-cp", "."
Also, classpath will use the colon separator on non-windoze platforms, fyi.
You should not have the full path information of the class file you want to run, because it is supposed to be found on your classpath, so instead of "C:/JButtons", you should have "JButtons".
good luck!
12-13-2004, 11:49 AM
freesoft_2000
Hi everyone,
i am really so sorry for bothering you doredson but this is what i did
Code:
String[] str12 ={Rstr,
"-cp" ,
"C:/" ,
";" ,
"." ,
"JButtons"};
// Rstr is the location of my java intrepreter
// The value of Rstr is C:/j2sdk1.4.2_04/bin/java
// C:/JButtons is the location of the class i want to run but can't
// seem to although the class exists at that location
I tried running the class but still the class is not run. Are the arguments for my string correct. If not then what is the correct way. Am i missing something?
Once again so sorry for disturbing you like that doredson
I hope someone can help me with this problem
Any help is greatly appreciated
Thank You
Yours Sincerely
Richard West
12-13-2004, 12:26 PM
doredson
Put your entire classpath as a single string.
"-cp", ".;c:/"
12-15-2004, 10:20 AM
doredson
FYI, the reason this will not work:
String[] str12 ={Rstr,"-cp" ,"C:/" ,";" ,"." , "JButtons"};
is the same reason that this will not work on the command line:
each string is an argument to the binary, "java" and java -h says the argument to -cp should be a single string, not multiple strings
hope that clarifies.
12-15-2004, 02:57 PM
freesoft_2000
Hi everyone,
I finally got it working but i could not have done without your help so let me say thank you doredson so much for help in helping me get my concepts correct.