Solved

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

Posted on 2010-11-29
20
1,326 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

757 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

23 Experts available now in Live!

Get 1:1 Help Now