Solved

Compiling & Running Java on My IDE

Posted on 2003-12-01
71
903 Views
Last Modified: 2013-12-14
Hello Again Experts;

On my developement of my very own Java IDE I had a problem on the compiling &
running process :

Problem 1:

Here is what I use for compiling :

try
   {
    String java = "c:\javac\bin\javac";
    String file1 = "c:\Michael\Cubej.java";

    Runtime r = Runtime.getRuntime(java"+"file1);
    r.waitFor();
    t1.setText("Result : "+r.exitValue());
    }

Catch(Exception e) {
    t1.setText(""+e);
}

// Where t1 is my JTextPane

The problem in this codes above it does compile my program but it takes a long time to finish &
I think it doubles the compilation process is there someway to close this after compilation.

Something like : r.close(); I tried it but it doesnt work. I just dont want my process to take
a long time to finish.


Problem 2:

How do I display on my JTextPane, what the dos promt does when compiling and running my
application ? is there any method for that ?


Problem 3:

How about on running. How do I call the classes and run it on my very own IDE. I tried
something like this but it does not work. Any other solution ?

try
   {
    String java = "c:\javac\bin\java";
    String file1 = "c:\Michael\Cubej";

    Runtime r = Runtime.getRuntime(java"+"file1);
    r.waitFor();
    t1.setText("Result : "+r.exitValue());
    }

Catch(Exception e) {
    t1.setText(""+e);
}

Hope to here from you experts
Thanks : Javatm
 
0
Comment
Question by:Javatm
  • 36
  • 35
71 Comments
 
LVL 92

Expert Comment

by:objects
ID: 9854196
1.you don't need to close it.
though you don't need to use exec to do compile.
0
 
LVL 92

Assisted Solution

by:objects
objects earned 500 total points
ID: 9854206
2. you need to read the stdout/stderr of the process.
The basic gist is:
Runtime r = Runtime.getRuntime();
Process p = r.exec("<your exe>");
BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
p.waitFor();
String s;
String response = "";
while ((s = in.readLine()) != null) {
     response += s;              
}

System.out.println(response);

But you should do the reading in a seperate thread.
0
 
LVL 92

Expert Comment

by:objects
ID: 9854208
3. I actually have a tool that does that if you are interested:
http://www.objects.com.au/products/jstart

I just use it mainly for testing but you may be interested in having a look.
0
 
LVL 92

Expert Comment

by:objects
ID: 9854215
0
 
LVL 92

Expert Comment

by:objects
ID: 9854229
sun.tools.javac.Main is the class used to compile.
0
 
LVL 15

Author Comment

by:Javatm
ID: 9854271
>> sun.tools.javac.Main is the class used to compile.

      How do I use it ?

      On your response no 2 : is it for running ? or compiling ?
0
 
LVL 92

Expert Comment

by:objects
ID: 9854307
> How do I use it ?

Here's some (minimal) javadoc. will see what else I can find.
http://www.cis.ohio-state.edu/~gb/cis788.07r/javac/sun.tools.javac.Main.html

> On your response no 2 : is it for running ? or compiling ?

Could be either, whenever you want to access the stdout/stderr of the process.
0
 
LVL 92

Expert Comment

by:objects
ID: 9854314
0
 
LVL 15

Author Comment

by:Javatm
ID: 9854356
Thanks I'm gonna try your codes okey.

Anyways is it okey if I just do it like this for dos simulation on my
question number 2, on dos in textpane :

t1.setText("c:\javac\bin\javac "+file1+");

//A fake dos but it does it fast & easy :)
0
 
LVL 92

Expert Comment

by:objects
ID: 9854366
> t1.setText("c:\javac\bin\javac "+file1+");

Sorry I thought you meant you wanted to display the *output* of javac :)
0
 
LVL 15

Author Comment

by:Javatm
ID: 9854430
>> Sorry I thought you meant you wanted to display the *output* of javac :)

     Thats correct so that I could show the users on every errors that they have
     and it should be displayed on the textpane.

      Do you have a much faster and easier way than to use this :
      http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_20775728.html
0
 
LVL 92

Expert Comment

by:objects
ID: 9854468
Thats the only way, ie. get the output stream from the Process and read the data from it and display in your text area.
That codes fairly generic so you should be able to simplify it for your case.
All you really need to do is the follwoing from a seperate thread:

while ((s = in.readLine()) != null)
{
     textarea.append(s);              
     textarea.append('\n');              
}
0
 
LVL 15

Author Comment

by:Javatm
ID: 9854586
Is it something like :

Runtime r = Runtime.getRuntime();
Process p = r.exec("<your exe>");

BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
p.waitFor();
String s;
String response = "";

while ((s = in.readLine()) != null) {
     t1.append(s);              
     t1.append('\n');                          
}
0
 
LVL 92

Expert Comment

by:objects
ID: 9854613
sort of you'll just need to handle the reading from a seperate thread, otherwise nothing will be displayed until the process has completed.
And you can also have problems with the process output blocking the process.
0
 
LVL 92

Expert Comment

by:objects
ID: 9854617
0
 
LVL 15

Author Comment

by:Javatm
ID: 9854706
>> Process p = r.exec("<your exe>");

     One more thing this is new to me. If my Cubej.java is on my folder like
     c:\Michael\Cubej.java how do I use the process for me to compile it ?
 
     is it like :
   
     String java = "c:\javac\bin\javac";
     Runtime r = Runtime.getRuntime(java);
     Process p = r.exec("c:\Michael\Cubej.java");

0
 
LVL 92

Assisted Solution

by:objects
objects earned 500 total points
ID: 9854769
String java = "c:\javac\bin\javac";
Runtime r = Runtime.getRuntime();
Process p = r.exec(java+" c:\Michael\Cubej.java");
0
 
LVL 15

Author Comment

by:Javatm
ID: 9854803
How about for running is it like :

String java = "c:\javac\bin\java";
Runtime r = Runtime.getRuntime();
Process p = r.exec(java+" c:\Michael\Cubej");
0
 
LVL 92

Expert Comment

by:objects
ID: 9854942
no, to run you need to specify the class name, not the file name.
0
 
LVL 15

Author Comment

by:Javatm
ID: 9854996
Oh is it like :

String java = "c:\javac\bin\java";
Runtime r = Runtime.getRuntime();
Process p = r.exec(java+" c:\Michael\Cubej.class");

0
 
LVL 15

Author Comment

by:Javatm
ID: 9855751
What if the file has more than one class ?
0
 
LVL 92

Expert Comment

by:objects
ID: 9855793
Not sure what you mean.
Each class file only contains one class.
And java.exe expects a class name (containing a static main method) to execute.
How do you start your IDE?
0
 
LVL 92

Expert Comment

by:objects
ID: 9855797
Instead of using exec your other option would be to call the classes main method directly, but not sure this is advisable as you would probably want to run the app in its own vm.
0
 
LVL 15

Author Comment

by:Javatm
ID: 9855949
I just run the Java IDE as :

c:\javac\bin\java Cubej

I have several classes on Cubej. Its nothing to do with the ide.

My problem is :

If the user created program & the program has many
classes how do i run it. Do I call the main class like :

String java = "c:\javac\bin\java";
Runtime r = Runtime.getRuntime();
Process p = r.exec(java+" c:\Michael\Sample.class");
0
 
LVL 92

Expert Comment

by:objects
ID: 9856179
Cubej is your classname is it not?

> If the user created program & the program has many
> classes how do i run it. Do I call the main class like :

Same way as you run any Java class from the command line:

java <classname>

0
 
LVL 15

Author Comment

by:Javatm
ID: 9856225
I'm gonna try it tonight philippine time.

Thanks you've been a great help . . .
As a part of experts-exchange and a
big help for me. I would like to give
you a progress report.

I'm naming the Java IDE as Cube - J;

Here is what has done so far :

1.) Tip of the day.
2.) Main GUI screen.
3.) Create, Open, Save & Print.
4.) Cut, Copy, Paste & Select All.
5.) JPopupMenu Enabled
6.) JTabbedPane w/ TextPane.

Still in Progress :

1.) Compiling & Running.

Hoping to add the following :

1.) Drag & Drop Capability.
2.) Keyword Highlighted
3.) Auto Complete function.
4.) Addin Plugins.
5.) Help Files.


The Main Advantage is : Speed

Thanks Again the " King ".
Friend : Javatm
0
 
LVL 15

Author Comment

by:Javatm
ID: 9886173
Hello Objects;

I had a problem here's what I've done :

// would not run w/o try & catch

try {
String java = "c:\javac\bin\java ";
Runtime r = Runtime.getRuntime();
Process p = r.exec(java+" c:\Michael\Cubej.class");
}
catch(Exception x) {
System.out.println(x);
}

The problem with this is that it puts a 20+ process on taskmanger on windows xp
and it wouldnt stop until I end then all, whats the problem ?

I didnt add this yet :

BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
p.waitFor();

String s;
String response = "";

while ((s = in.readLine()) != null) {
     t1.append(s);              
     t1.append('\n');                        
}
0
 
LVL 92

Expert Comment

by:objects
ID: 9886282
well for one you're still passing the vm a filename, when it is expecting classname.

>  it wouldnt stop until I end then all

what wouldn't stop?
0
 
LVL 15

Author Comment

by:Javatm
ID: 9887045
>> well for one you're still passing the vm a filename, when it is expecting classname.
   
      What do you mean I passed the classname which is Cubej.class ?

>> what wouldn't stop?

      The process it wouldnt stop, it had 20+ process on task manager when I followed this :

      String java = "c:\javac\bin\java ";
      Runtime r = Runtime.getRuntime();
      Process p = r.exec(java+" c:\Michael\Cubej.class");



0
 
LVL 15

Author Comment

by:Javatm
ID: 9887085
Oh dont mind it just tell me if this is correct :

String java = "c:/javac/bin/javac.exe";
String file = "c:/Michael/Cubej.java";

Runtime r = Runtime.getRuntime();
Process p = r.exec(java+""+file);
p.waitFor();

BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
p.waitFor();

String s;

while ((s = in.readLine()) != null) {
     t1.append(s);              
     t1.append('\n');                        
}
0
 
LVL 92

Expert Comment

by:objects
ID: 9890305
the compile command is ok.

you'll need to read the process output in a new thread and start that thread before you call waitFor(). Reason being that the process can get blocked by output fill ing the buffer.
0
 
LVL 15

Author Comment

by:Javatm
ID: 9900515
objects;

Hello friend, compiling is okey :) but when I tried it in a run process it wouldnt run the program
here is a sample :

String java = "c:/javac/bin/java.exe";
String file = "c:/Michael/Sample.class";

Runtime r = Runtime.getRuntime();
Process p = r.exec(java+""+file);
p.waitFor();

What was wrong ?
Javatm


0
 
LVL 92

Expert Comment

by:objects
ID: 9900533
One more time, java.exe expects a classname and *not* a filename :)

assuming that class is not in a package the command would be:

java -classpath c:\Michael Sample
0
 
LVL 15

Author Comment

by:Javatm
ID: 9901466
Thanks but how do I implement it ? can you give me a sample ?

is it like :

String java = "c:/javac/bin/java.exe";
String file = "c:/Michael/Sample";

Runtime r = Runtime.getRuntime();
Process p = r.exec(java+""+file);
0
 
LVL 92

Expert Comment

by:objects
ID: 9901505
Runtime r = Runtime.getRuntime();
Process p = r.exec("java -classpath c:\\Michael Sample");
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 15

Author Comment

by:Javatm
ID: 9901578
Is this the final process for running :

try {
Runtime r = Runtime.getRuntime();
Process p = r.exec("java -classpath c:\\Michael Sample");
}

catch(Exception e) {
System.out.println(""+e);
}    

No other things like declaring strings for the path ?
     
     
0
 
LVL 92

Expert Comment

by:objects
ID: 9901593
sorry, yes thats assuming it can find java.exe. Best to specify the location:

Process p = r.exec("c:/javac/bin/java -classpath c:\\Michael Sample");
0
 
LVL 15

Author Comment

by:Javatm
ID: 9901624
For example I have a one .java program named : Sample.java
Lest say it has one class named: Sample.class

All I need to do to is to run this command on the Java IDE
that I'm creating :

try {
Runtime r = Runtime.getRuntime();
Process p = r.exec("c:/javac/bin/java -classpath c:\\Michael Sample");
}

catch(Exception e) {
System.out.println(""+e);
}    

Correct ?
0
 
LVL 92

Expert Comment

by:objects
ID: 9901704
correct :)
0
 
LVL 15

Author Comment

by:Javatm
ID: 9901760
Is there any way to auto detect the path of the java folder so that
I'm not gonna specify that on my own ?
0
 
LVL 92

Expert Comment

by:objects
ID: 9901935
you could search the file system for java.exe.
though it is probably worth letting the user specify it as there may be >1 vm installed.
0
 
LVL 15

Author Comment

by:Javatm
ID: 9902023
Okey dude, thanks one last thing :

This does not work, It does not do anything

BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
String s;

while ((s = in.readLine()) != null) {
     t1.append(s);              
     t1.append('\n');                        
}

So I tried it like this :

BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
String s = "" + in.readLine();
t1.append(s);              

What it only display something like : java.out.r454rcf
I'm I doing it wrong ?

I really need to extract the error messages when I do the compile time.



0
 
LVL 92

Assisted Solution

by:objects
objects earned 500 total points
ID: 9902045
the error message may be being sent to stderr, try reading from p.getErrorStream().
0
 
LVL 15

Author Comment

by:Javatm
ID: 9902060
Okey is it like :

BufferedReader in = new BufferedReader(new InputStreamReader(p.getErrorStream()));
String s = "" + in.readLine();
t1.append(s);              

0
 
LVL 92

Expert Comment

by:objects
ID: 9902067
no, what u had originally:

while ((s = in.readLine()) != null) {
     t1.append(s);              
     t1.append('\n');                        
}

0
 
LVL 15

Author Comment

by:Javatm
ID: 9902073
What I mean is like :

BufferedReader in = new BufferedReader(new InputStreamReader(p.getErrorStream()));
String s;

while ((s = in.readLine()) != null) {
     t1.append(s);              
     t1.append('\n');                        
}
0
 
LVL 15

Author Comment

by:Javatm
ID: 9902171
Okey here's what I've got.

// Compile Method

private void compile {

try {
          String java = "c:/javac/bin/javac.exe";
          String file = "c:/Michael/Sample.java";

          Runtime r = Runtime.getRuntime();
          Process p = r.exec(java+""+file);
          p.waitFor();
   
         BufferedReader in = new BufferedReader(new InputStreamReader(p.getErrorStream()));
         p.waitFor();

         String s;

while ((s = in.readLine()) != null) {

         t1.append(s);              
         t1.append('\n');              
         
         }
}

catch(Exception e) {
         System.out.println(""+e);
         }

}

// Run Method

private void run {

try {
         Runtime r = Runtime.getRuntime();
         Process p = r.exec("c:/javac/bin/java -classpath c:\\Michael Sample");
     }

catch(Exception e) {
         System.out.println(""+e);
         }    

}

Is that it ?
Do you think I'm ready to do ?
0
 
LVL 92

Expert Comment

by:objects
ID: 9902181
you should be reading the process output from a seperate thread, otherwise the buffer could fill and block the process.

http://www.javaworld.com/javatips/jw-javatip131.html
0
 
LVL 15

Author Comment

by:Javatm
ID: 9902231
Okey I'm gonna do what you've suggested anyways, I'm gonna test it again,
sorry if it takes long, thank you very much :)

0
 
LVL 92

Expert Comment

by:objects
ID: 9902254
no worries :)
0
 
LVL 15

Author Comment

by:Javatm
ID: 9980800
Dear Friend;

I already tried your suggestion about the reading of errors and the running portion but
some how the reading of errors & the running portion does not work, what
do I do about it ?

Javatm
0
 
LVL 92

Expert Comment

by:objects
ID: 9982190
how exactly is it not working?
0
 
LVL 15

Author Comment

by:Javatm
ID: 9982623
I just followed the codes that you suggested

Here is for reading the errors :

// This is not yet on seperate thread because I'm
// still testing it if it will read the errors while compiling
// but it does not show anything

BufferedReader in = new BufferedReader(new InputStreamReader(p.getErrorStream()));
         p.waitFor();

         String s;

while ((s = in.readLine()) != null) {

         t1.append(s);              
         t1.append('\n');              
         
         }

Here is for running :

// In here it does not run the sample program

Runtime r = Runtime.getRuntime();
Process p = r.exec("c:/javac/bin/java -classpath c:\\Michael Sample");

What did I did wrong, can you try it your self so that you'll know
the errors, thanks.

Javatm

0
 
LVL 92

Expert Comment

by:objects
ID: 9982638
Thats not what I suggested. I said you need to read the output from a seperate thread

> you should be reading the process output from a seperate thread, otherwise the
> buffer could fill and block the process.
> http://www.javaworld.com/javatips/jw-javatip131.html
0
 
LVL 15

Author Comment

by:Javatm
ID: 9982894
Ok how about the running? thanks.
0
 
LVL 15

Author Comment

by:Javatm
ID: 9982975
I tried reading the errors in a separate thread but it does not work :

// Here is the compiling & I'm reading
// the error in a separate thread
 
 try {

  String java = "c:/javac/bin/javac.exe";

  r = Runtime.getRuntime();
  p = r.exec(java+" "+fileName);

  Errors read = new Errors();
  Thread printThread = new Thread(read);
  printThread.start();

  }
  catch(Exception e) {
  }

// Method for reading the errors

  private void readErrors()
  {
  try {

  // Are your sure this is correct ?
  BufferedReader in = new BufferedReader(new InputStreamReader(p.getErrorStream()));
  p.waitFor();

  String s;

  // It does not display any errors
  while ((s = in.readLine()) != null) {
  t3.setText(s);          

  // I'm using this to know if the thread
  // runs or not, the thread runs.    
  t3.setText("Did not worked !");      

  }          
  }
  catch(Exception e) {}  
  }

// ReadErrors Class

  class Errors implements Runnable {
  public void run() {
  readErrors();
  }
  }

So why its not working ?
Javatm
0
 
LVL 92

Assisted Solution

by:objects
objects earned 500 total points
ID: 9983175
> p.waitFor();

You shouldn't be waiting in the thread reading the ouput. The purpose of doing it in a seperate thread is to read the output while the the process is executing. You do not want to wait until the process completes for the reasons given in the article posted above.
0
 
LVL 15

Author Comment

by:Javatm
ID: 9983593
Very good My friend, it worked thank you so much.
can you end this question.

My last problem would be the running as I said
it does not worked 2.
0
 
LVL 15

Author Comment

by:Javatm
ID: 9988604
Still there ???
0
 
LVL 92

Expert Comment

by:objects
ID: 9988745
yes I'm still there.
Sorry was that a question about running, it should be the same as compiling just with a different command.
0
 
LVL 15

Author Comment

by:Javatm
ID: 9988792
How do I do it ???
0
 
LVL 92

Expert Comment

by:objects
ID: 9988817
just change the command you pass to exec() to whatever is needed to run the class.
0
 
LVL 15

Author Comment

by:Javatm
ID: 9988891
what do you mean I tried this :

Runtime r = Runtime.getRuntime();
Process p = r.exec("c:/javac/bin/java -classpath c:\\Michael Sample");

or this :

String java = "c:/javac/bin/java.exe";
String file = "c:/Michael/Sample.class";

Runtime r = Runtime.getRuntime();
Process p = r.exec(java+"  "+file);

but all of this does not work, can you give a sample or a correct way to solve
this problem, this will be the last problem.
0
 
LVL 92

Accepted Solution

by:
objects earned 500 total points
ID: 9989141
Sorry I assumed you knew how to run a class from the command line, the usage is:

java -classpath <classpath> <classname>

So in your example above the command would be (assuming Sample is in package Michael) the first command you had was correct:

java -classpath c:\Michael Sample

Do java -help for full syntax.
0
 
LVL 15

Author Comment

by:Javatm
ID: 9989181
Ok so this should be okey then :

Runtime r = Runtime.getRuntime();
Process p = r.exec("c:/javac/bin/java -classpath c:\Michael Sample");

Correct ???
0
 
LVL 92

Expert Comment

by:objects
ID: 9989190
easy to test, just run it from the command line.
0
 
LVL 15

Author Comment

by:Javatm
ID: 9989210
>> easy to test, just run it from the command line.

      I'm actually at work now so I can not run it here because I'm
      not allowed anyways I'm gonna test it again and lets see.

      I'm totally over joyed w/ this software, its professionally designed
      over all I'm still using 1 .java program for this IDE w/ over 35
      classes, what do you think is it okey if I devide it in a  different
      .java programs ?
0
 
LVL 15

Author Comment

by:Javatm
ID: 9999707
Hello;

// Okey it worked but this is invoking the file directly for it to run
Process p = r.exec("c:/javac/bin/java -classpath c:\Michael Sample");

What I need to do is to open a file using a JFileChooser run it.
Here's what I did :

// fileName is the file which is c:\Michael\Sample.java
Process p = r.exec("c:/javac/bin/java -classpath "+fileName);

It doesnt work because fileName holds the complete file including the .java
is there anyway I could remove the .java on it so that it will just hold the
base folder and the file name ?

Thanks, Merry Christmas.
Friend : Javatm








0
 
LVL 92

Expert Comment

by:objects
ID: 10000893
It's not that simple, you need to also determine whether that class is located in a package or not, and what that package is.

eg. if in your exacmple above Sample was in a package Michael then the command needed would be:

c:/javac/bin/java -classpath c:\ Michael.Sample
0
 
LVL 15

Author Comment

by:Javatm
ID: 10001101
Okey I'm gonna close this question
thanks :) Javatm
0
 
LVL 92

Expert Comment

by:objects
ID: 10001119
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
nestparen challenge 4 56
create a gui in perl 3 42
JDeveloper 12c for 32 bit 4 34
Java passing arguments (type error) 2 33
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This video teaches viewers about errors in exception handling.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now