Solved

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

Posted on 2010-11-29
20
1,335 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This video teaches viewers about errors in exception handling.

777 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