• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

executing windows command from Java code

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);
}
 
Bartender
Posts: 1166
17
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 1776
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And welcome to Javaranch, abhijit karmakar
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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'?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic