Solved

how do i run the java program using runtime.getruntime().exec

Posted on 2010-11-29
20
1,348 Views
Last Modified: 2012-05-10
The following code does not work. if jFileChooser1.getSelectedFile() is replaced by the class file it works.

p= Runtime.getRuntime().exec("c:\\cygwin\\bin\\bash.exe -c 'cd \"/cygdrive/c/NewFolder\"; \"/cygdrive/c/Program Files/Java/jdk1.6.0_21/bin/java jFileChooser1.getSelectedFile()\" '"  );
0
Comment
Question by:siddharthkool
  • 8
  • 7
  • 2
  • +2
20 Comments
 
LVL 8

Expert Comment

by:robthewolf
ID: 34231898
what you are saying is that the class file com.abc.MyClass is not equivalent to jFileChooser1.getSelectedFile()
try printing jFileChooser1.getSelectedFile() to the console and see if it is what you expect it to be.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 34232368
>>if jFileChooser1.getSelectedFile() is replaced by the class file it works.

That's not the case.  The program java.exe takes as its principal argument a class name, not a file name, so the only way you're going to get a file to 'run' is if:

a. The extension (.class) is removed
b. The default package (i.e. 'no package') is used

I'm not sure why you're running it under Cygwin bash - that will only complicate the issue. You can invoke java.exe directly (subject to the provisos i mentioned)
0
 

Author Comment

by:siddharthkool
ID: 34232400
jFileChooser1.getSlectedFile() prints: the full path.

So i tried using-
Runtime r=Runtime.getRuntime();
                   
                   //System.out.println(jFileChooser1.getSelectedFile());
                    String[] cmd={"c:\\cygwin\\bin\\bash.exe", "-c", "'/cygdrive/c/Program Files/Java/jdk1.6.0_21/bin/java '"  + jFileChooser1.getSelectedFile()};
                    p=r.exec(cmd);

this is the output i get----
Output....
C:\JDBCRegressionBugs\ConnectACSBitColumn.class
[Stderr] /usr/bin/bash: /cygdrive/c/Program Files/Java/jdk1.6.0_21/bin/java null: No such file or directory
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 86

Expert Comment

by:CEHJ
ID: 34232417
Did you read my last comment at all?
0
 

Author Comment

by:siddharthkool
ID: 34232577
@cehj
 this works but how do i remove the .class extension?
System.out.println("Output....");
                   System.out.println(jFileChooser1.getName(jFileChooser1.getSelectedFile()).toString());
                   Runtime r=Runtime.getRuntime();
                   
                   String[] cmd={"'/cygdrive/c/Program Files/Java/jdk1.6.0_21/bin/java '"  + jFileChooser1.getName(jFileChooser1.getSelectedFile())};
                    p=r.exec(cmd);
0
 

Author Comment

by:siddharthkool
ID: 34232583
Output....
ConnectACSBitColumn.class
java.io.IOException: Cannot run program "'/cygdrive/c/Program\Files/Java/jdk1.6.0_21/bin/java 'ConnectACSBitColumn.class":
0
 
LVL 2

Expert Comment

by:astaplesnerd
ID: 34232644
Use substring to remove .class from the file name.
if (fileName.endWith(".class")
    fileName = fileName.substring(0, fileName.length() - 6)

http://download.oracle.com/javase/1.5.0/docs/api/java/lang/String.html#substring(int,%20int)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 34232717

String fname = jFileChooser1.getSelectedFile().getName();
if(fname.endsWith(".class")) {
    fname = fname.replaceAll("\.class$", "");
    // Exec it
}
else {
   // Error
}

Open in new window

0
 

Author Comment

by:siddharthkool
ID: 34233327
@CEHJ:
String fname = jFileChooser1.getSelectedFile().getName();
                    if(fname.endsWith(".class")) {
                    fname = fname.replaceAll(".class$", "");
                     // Exec it
                    Runtime r=Runtime.getRuntime();

                    String[] cmd={"'/cygdrive/c/Program Files/Java/jdk1.6.0_21/bin/java '"  + fname};
                    p=r.exec(cmd);
                    }
                   else{
                    System.out.println("error");
                     }

This gives fname not found. how d i change this so that this gives finds the path to the fname?
                   
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 34233409
Please post the exact error, pasting it
0
 

Author Comment

by:siddharthkool
ID: 34233607
Output....
ConnectACSBitColumn.class
java.io.IOException: Cannot run program "'/cygdrive/c/Program Files/Java/jdk1.6.0_21/bin/java 'ConnectACSBitColumn": CreateProcess error=2, The system cannot find the file specified
0
 

Author Comment

by:siddharthkool
ID: 34234152
@CEHJ:

im giving this n it is not throwing error n also not giving output

p= Runtime.getRuntime().exec("c:\\Windows\\system32\\cmd.exe -c 'cd \"/C:/JDBC\"; \"/C:/Program Files/Java/jdk1.6.0_21/bin/java \" '"  +fname);
0
 
LVL 8

Expert Comment

by:robthewolf
ID: 34234202
If you type out what you need on the commandline does it work?
if so you need to make sure that you can print out exactly the same line, if so passing that string to exec() should work.
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 50 total points
ID: 34234499
All you need is the following (the java bin directory being in your PATH and 'name' being a class name
Process p = Runtime.exec(new String[] { "java", name });

Open in new window

0
 
LVL 92

Expert Comment

by:objects
ID: 34234509
you should be using something more like this:

r.exec(new String[] { "/cygdrive/c/Program Files/Java/jdk1.6.0_21/bin/java", "-cp .", jFileChooser1.getSelectedFile().getName().replaceAll(".class", "")});
0
 

Author Comment

by:siddharthkool
ID: 34313039
p= Runtime.getRuntime().exec("c:\\Windows\\System32\\cmd.exe -c 'cd \"/C:/JDBC\"; \"/C:/Program Files/Java/jdk1.6.0_21/bin/java Connectadd\" '");

plz suggest an alternative that will work???
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 34313174
Please post the code that works from the command line. Hardcoding paths to a java runtime is generally not a good idea. Does the following work at the command line?
java -version

Open in new window

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 34313647
With your path set correctly, the above and the below, should run fine:
final String[] COMMAND = { "cmd.exe", "/c", "java", "-cp", "C:\\JDBC", "Connectadd" };
p= Runtime.getRuntime().exec(COMMAND);

Open in new window

0
 
LVL 92

Expert Comment

by:objects
ID: 34313725
> p= Runtime.getRuntime().exec("c:\\Windows\\System32\\cmd.exe -c 'cd \"/C:/JDBC\"; \"/C:/Program Files/Java/jdk1.6.0_21/bin/java Connectadd\" '");

you cannot cd, as there is no shell
instead you need to specify the classpath as I posted in my earlier comment


p= Runtime.getRuntime().exec(new String[] { "cmd.exe", "-c","java", "-cp", "C:\\JDBC",  "Connectadd"});
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 34685173
:)
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
by zero exception 10 69
Running JavaFX on the Raspberry Pi 27 121
dao vs facade design patterns 2 29
Java List 4 32
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
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:

713 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