Solved

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

Posted on 2010-11-29
20
1,343 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
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.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
web services creation SOAP vs REST 5 56
Java: The Public Class Main 4 32
SequenceInputStream example 3 19
Java class and jar 3 22
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

856 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