• 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

Running external program using java code

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello
i am going to run "notepad.exe" with java code; by exploring internet i found this. it runs successful but doesnt run "notepad.exe"
this is the code:

 
Ranch Hand
Posts: 499
Spring AngularJS Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
omid azadi,
Add stack trace to your exception and see if any error is thrown.
 
Saloon Keeper
Posts: 27807
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Partheban Udayakumar wrote:omid azadi,
Add stack trace to your exception and see if any error is thrown.



... And when you do, you'll almost certainly see that the file "Notepad.exe" could not be found.

It's always a good idea to put the full pathname of any executable that you want Java (or cron or whatever) to run for you. Otherwise it's possible that the execution path being used won't be what you think it is and probably won't contain the program.

Swallowing up Exceptions instead of reporting them (or letting the JVM report them) is one of the top 10 ways to earn my wrath. Be glad I don't work where you do!
 
omid azadi
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
tanx partheban Udayakumar & Tim Holloway
i am very new to java
i dont know how to add stack trace exception please help me.
what is the package name for Stack Trace Exception?
 
omid azadi
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, Tim holloway
using full path worked
-----------------------------
still dont know how to add stack trace Exception , what is this exception? when it happens ?
 
Tim Holloway
Saloon Keeper
Posts: 27807
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


This is an Exception handler. If an IOException is thrown, then whatever exists in the dependent clause will be executed. Which in your case was no code at all, thereby ensuring that the information in the Exception object "e" was lost.

A better way would have been:


That would cause the stack trace for the exception to print on stdout (System.out).
 
omid azadi
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks
-----------------------------
sorry for my dumb questions again. I dont understand these bold words in this text , and i need clarification.
The java.lang.Runtime.exec(String[] cmdarray, String[] envp) method executes the specified command and arguments in a separate process with the specified environment. This is a convenience method. An invocation of the form exec(cmdarray, envp) behaves in exactly the same way as the invocation exec(cmdarray, envp, null).
-------------------------------
And what is envp for?
 
Marshal
Posts: 79239
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The envp parameter is a pointer to the runtime environment you intend to use.

Never go anywhere near Runtime.exec() until you understand the classic article by Michael Daconta, and you know about ProcessBuilders.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic