Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 919
  • Last Modified:

Compiling & Running Java on My IDE

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
Javatm
Asked:
Javatm
  • 36
  • 35
5 Solutions
 
objectsCommented:
1.you don't need to close it.
though you don't need to use exec to do compile.
0
 
objectsCommented:
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
 
objectsCommented:
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
objectsCommented:
0
 
objectsCommented:
sun.tools.javac.Main is the class used to compile.
0
 
JavatmAuthor Commented:
>> 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
 
objectsCommented:
> 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
 
JavatmAuthor Commented:
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
 
objectsCommented:
> t1.setText("c:\javac\bin\javac "+file1+");

Sorry I thought you meant you wanted to display the *output* of javac :)
0
 
JavatmAuthor Commented:
>> 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
 
objectsCommented:
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
 
JavatmAuthor Commented:
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
 
objectsCommented:
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
 
objectsCommented:
0
 
JavatmAuthor Commented:
>> 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
 
objectsCommented:
String java = "c:\javac\bin\javac";
Runtime r = Runtime.getRuntime();
Process p = r.exec(java+" c:\Michael\Cubej.java");
0
 
JavatmAuthor Commented:
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
 
objectsCommented:
no, to run you need to specify the class name, not the file name.
0
 
JavatmAuthor Commented:
Oh is it like :

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

0
 
JavatmAuthor Commented:
What if the file has more than one class ?
0
 
objectsCommented:
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
 
objectsCommented:
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
 
JavatmAuthor Commented:
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
 
objectsCommented:
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
 
JavatmAuthor Commented:
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
 
JavatmAuthor Commented:
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
 
objectsCommented:
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
 
JavatmAuthor Commented:
>> 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
 
JavatmAuthor Commented:
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
 
objectsCommented:
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
 
JavatmAuthor Commented:
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
 
objectsCommented:
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
 
JavatmAuthor Commented:
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
 
objectsCommented:
Runtime r = Runtime.getRuntime();
Process p = r.exec("java -classpath c:\\Michael Sample");
0
 
JavatmAuthor Commented:
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
 
objectsCommented:
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
 
JavatmAuthor Commented:
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
 
objectsCommented:
correct :)
0
 
JavatmAuthor Commented:
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
 
objectsCommented:
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
 
JavatmAuthor Commented:
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
 
objectsCommented:
the error message may be being sent to stderr, try reading from p.getErrorStream().
0
 
JavatmAuthor Commented:
Okey is it like :

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

0
 
objectsCommented:
no, what u had originally:

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

0
 
JavatmAuthor Commented:
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
 
JavatmAuthor Commented:
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
 
objectsCommented:
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
 
JavatmAuthor Commented:
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
 
objectsCommented:
no worries :)
0
 
JavatmAuthor Commented:
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
 
objectsCommented:
how exactly is it not working?
0
 
JavatmAuthor Commented:
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
 
objectsCommented:
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
 
JavatmAuthor Commented:
Ok how about the running? thanks.
0
 
JavatmAuthor Commented:
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
 
objectsCommented:
> 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
 
JavatmAuthor Commented:
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
 
JavatmAuthor Commented:
Still there ???
0
 
objectsCommented:
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
 
JavatmAuthor Commented:
How do I do it ???
0
 
objectsCommented:
just change the command you pass to exec() to whatever is needed to run the class.
0
 
JavatmAuthor Commented:
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
 
objectsCommented:
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
 
JavatmAuthor Commented:
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
 
objectsCommented:
easy to test, just run it from the command line.
0
 
JavatmAuthor Commented:
>> 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
 
JavatmAuthor Commented:
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
 
objectsCommented:
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
 
JavatmAuthor Commented:
Okey I'm gonna close this question
thanks :) Javatm
0
 
objectsCommented:
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 36
  • 35
Tackle projects and never again get stuck behind a technical roadblock.
Join Now