944,052 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 3427
  • Java RSS
You are currently viewing page 1 of this multi-page discussion thread
Nov 30th, 2004
0

Compiling And Running

Expand Post »
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
Similar Threads
Reputation Points: 25
Solved Threads: 10
Practically a Master Poster
freesoft_2000 is offline Offline
623 posts
since Jun 2004
Dec 2nd, 2004
0

Re: Compiling And Running

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.

Quote 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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
santa_barbarian is offline Offline
5 posts
since Dec 2004
Dec 4th, 2004
0

Re: Compiling And Running

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
Reputation Points: 25
Solved Threads: 10
Practically a Master Poster
freesoft_2000 is offline Offline
623 posts
since Jun 2004
Dec 10th, 2004
0

Re: Compiling And Running

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

Java Syntax (Toggle Plain Text)
  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
Reputation Points: 25
Solved Threads: 10
Practically a Master Poster
freesoft_2000 is offline Offline
623 posts
since Jun 2004
Dec 11th, 2004
0

Re: Compiling And Running

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.
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Dec 11th, 2004
0

Re: Compiling And Running

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.
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Dec 11th, 2004
0

Re: Compiling And Running

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
Reputation Points: 25
Solved Threads: 10
Practically a Master Poster
freesoft_2000 is offline Offline
623 posts
since Jun 2004
Dec 11th, 2004
0

Re: Compiling And Running

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.
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Dec 12th, 2004
0

Re: Compiling And Running

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

Java Syntax (Toggle Plain Text)
  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
Reputation Points: 25
Solved Threads: 10
Practically a Master Poster
freesoft_2000 is offline Offline
623 posts
since Jun 2004
Dec 12th, 2004
0

Re: Compiling And Running

you don't need to run the java runtime since you're already in it

Just load the class and call its main method.
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: CardLayout problem restated
Next Thread in Java Forum Timeline: line up JTextField GUI java & write to file





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC