Compiling And Running

Reply

Join Date: Jun 2004
Posts: 609
Reputation: freesoft_2000 is an unknown quantity at this point 
Solved Threads: 7
freesoft_2000 freesoft_2000 is offline Offline
Practically a Master Poster

Compiling And Running

 
0
  #1
Nov 30th, 2004
Hi everyone,

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
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 5
Reputation: santa_barbarian is an unknown quantity at this point 
Solved Threads: 0
santa_barbarian santa_barbarian is offline Offline
Newbie Poster

Re: Compiling And Running

 
0
  #2
Dec 2nd, 2004
TextPad is a freely available windows program that allows you to view your source code (the .java file) in one text area. There is an option in the tools menu to compile. The output from compiling is shown is different text area. There is another option on the tools menu to run your compiled code.

Basically java is compiled via the following command:
javac filename.java

The newly compiled code will have the name filename.class. It can now be run via the following command:
java filename

Any program, like textpad, can take advantage of these two commands to run java.

Originally Posted by freesoft_2000
Hi everyone,

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
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 609
Reputation: freesoft_2000 is an unknown quantity at this point 
Solved Threads: 7
freesoft_2000 freesoft_2000 is offline Offline
Practically a Master Poster

Re: Compiling And Running

 
0
  #3
Dec 4th, 2004
hi everyone,

santa_barbarian you said
"Basically java is compiled via the following command:
javac filename.java"

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
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 609
Reputation: freesoft_2000 is an unknown quantity at this point 
Solved Threads: 7
freesoft_2000 freesoft_2000 is offline Offline
Practically a Master Poster

Re: Compiling And Running

 
0
  #4
Dec 10th, 2004
Hi everyone,

Here is what i have done so far but 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

  1.  
  2. public void compile ()
  3. {
  4. byte[] buffer1 = new byte[2048];
  5. int len = 0;
  6. String str9 = "C:ddk1.4/bin/javac";
  7. String str10 = "D:gui.java";
  8. String[] str11 = {str9, str10};
  9. File File11 = new File("C://JIDE_Errors.TXT");
  10.  
  11. try
  12. {
  13. Runtime1 = Runtime.getRuntime();
  14. //The below command command line is where the exception
  15. //occurs saying it is a null pointer exception
  16.  
  17. Process1 = Runtime1.exec(str11);
  18. InputStream in = new BufferedInputStream(Process1.getErrorStream());
  19. FileOutputStream out = new FileOutputStream(File11);
  20.  
  21. while((len = in.read(buffer1)) != -1)
  22. {
  23. out.write(buffer1, 0, len);
  24. }
  25.  
  26. Process1.waitFor();
  27.  
  28. if(Process1.exitValue() == 0)
  29. {
  30. System.out.println("Compilation Successful");
  31. }
  32.  
  33. else
  34. {
  35.  
  36. }
  37.  
  38. }

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
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 213
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Compiling And Running

 
0
  #5
Dec 11th, 2004
NullPointerException is a RuntimeException which means you don't have to catch it in your code.
Therefore the compiler didn't complain.
Where did you get it? Figure that out and check whether whatever you're using there indeed isn't null before continuing.

If you don't know what a NullPointerException is I'd advise you to stick to simpler programs than what you're attempting to create for now.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 213
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Compiling And Running

 
0
  #6
Dec 11th, 2004
P.S. you may want to take a look at java.lang.Compiler which seems to provide a wrapper around the compiler enabling you to avoid all that messy Runtime.exec() which is not a very nice way to do things at all.
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 609
Reputation: freesoft_2000 is an unknown quantity at this point 
Solved Threads: 7
freesoft_2000 freesoft_2000 is offline Offline
Practically a Master Poster

Re: Compiling And Running

 
0
  #7
Dec 11th, 2004
Hi everyone,

i had a look at the java.lang.Compiler class and it seems to fit my needs but does java have a wrapper class that runs the compiled class as well. If the do where do i find the documentation for it.

Yours Sincerely

Richard West
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 213
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Compiling And Running

 
0
  #8
Dec 11th, 2004
You can execute any public method on any class from any other class.
So you could just call the main() method on your class.

Or more properly you'd instantiate another classloader and use that I guess.
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 609
Reputation: freesoft_2000 is an unknown quantity at this point 
Solved Threads: 7
freesoft_2000 freesoft_2000 is offline Offline
Practically a Master Poster

Re: Compiling And Running

 
0
  #9
Dec 12th, 2004
Hi everyone,

Sorry to bother you guys but what if i want to run the class file that i have already compiled.

Let's say if my compiled class is located at C:/JButtons.class
and if i want to run this class this is what i did

  1.  
  2. String[] str12 ={"C:/j2sdk1.4.2_04/bin/java", "C:/JButtons"};
  3.  
  4. Runtime Runtime1;
  5. Process Process1;
  6.  
  7. try
  8. {
  9. Runtime1 = Runtime.getRuntime();
  10. Process1 = Runtime2.exec(str12);
  11.  
  12. Process1.waitFor();
  13. }
  14.  
  15. catch(Exception e)
  16. {
  17. e.printStackTrace();
  18. }

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.

I hope some someone can help me with this problem

Any help is greatly appreciated

Thank You

Yours Sincerely

Richard West
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 213
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Compiling And Running

 
0
  #10
Dec 12th, 2004
you don't need to run the java runtime since you're already in it

Just load the class and call its main method.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC