• Post Reply Bookmark Topic Watch Topic
  • New Topic

Running external program using java code  RSS feed

 
omid azadi
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • 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:

 
Partheban Udayakumar
Ranch Hand
Posts: 499
AngularJS Java Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
omid azadi,
Add stack trace to your exception and see if any error is thrown.
 
Tim Holloway
Bartender
Posts: 18531
61
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • 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: 12
  • Mark post as helpful
  • send pies
  • 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: 12
  • Mark post as helpful
  • send pies
  • 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
Bartender
Posts: 18531
61
Android Eclipse IDE Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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: 12
  • Mark post as helpful
  • send pies
  • 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?
 
Campbell Ritchie
Sheriff
Posts: 53774
128
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!