• Post Reply Bookmark Topic Watch Topic
  • New Topic

executing windows command from Java code

 
abhijit karmakar
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Below Command working fine from command console:**
cmd.exe /b start /b C:\Progra~1\Git\bin\sh.exe --login -i /c "sh SimpleScript.sh"

But Below code snippet is not working!!! from java code:**
runtime generated command from java: cmd.exe /b start /b C:\Progra~1\Git\bin\sh.exe -- login -i /c "sh SimpleScript.sh"

ArrayList<String> AppArgList = new ArrayList<String>();
AppArgList.add("cmd.exe");
AppArgList.add(" /b");
AppArgList.add(" start /b " + "C:\\Progra~1\\Git\\bin\\sh.exe"+ " -- login" + " -i");
AppArgList.add(" /c "+"\"sh SimpleScript.sh\"");


String[] arguments1 = AppArgList.toArray(new String[AppArgList.size()]);
System.out.println("Printing argument..........."+arguments1[0]+arguments1[1]+arguments1[2]+arguments1[3]);
Process proc1 = null;
Runtime rt = null;

try {
rt= Runtime.getRuntime();
proc1 =rt.exec(arguments1);
}
 
Richard Tookey
Bartender
Posts: 1166
17
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm a little lost as to why you need to invoke a shell from cmd.exe; I would expect you to just invoke the shell command. Could you elaborate on this?

If you have not done so, please read ALL the sections of http://www.javaworld.com/jw-12-2000/jw-1229-traps.html and implement ALL the recommendations. You may get away without implementing the recommendations but a some point the traps will jump up and bite you.
 
John Jai
Rancher
Posts: 1776
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And welcome to Javaranch, abhijit karmakar
 
Tony Docherty
Saloon Keeper
Posts: 3138
72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
AppArgList.add(" start /b " + "C:\\Progra~1\\Git\\bin\\sh.exe"+ " -- login" + " -i");

Is there supposed to be a whitespace between '--' and 'login'?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!