Solved

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

Posted on 2010-11-29
20
1,355 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This video teaches viewers about errors in exception handling.
Suggested Courses

632 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