File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Running executables - one works; one doesn't Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Running executables - one works; one doesn Watch "Running executables - one works; one doesn New topic
Author

Running executables - one works; one doesn't

Lori Cook
Greenhorn

Joined: Apr 29, 2013
Posts: 4
Googled, did a search on this site and read nice article www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html, but still didn't find anything that helps.

Problem: Trying to run Remote Desktop and Telnet by using Runtime.getRuntime().exec().
Results:
Running Remote Desktop works.
Running Telnet generates a "java.io.IOException: Cannot run program "C:\Windows\System32\telnet.exe": CreateProcess error=2, The system cannot find the file specified"

Facts;
telnet.exe and mstsc.exe both exist in C:/Windows/System32.
To spawn Remote Desktop, the string passed into the method below is "mstsc.exe /f /v:", "ip_address" set to the IP address of the system and "additional" is empty.
To spawn Telnet, the string passed into the method below is "telnet.exe ", "ip_address" set to the IP address of the system and "additional" is the port desired.
"telnet.exe ip_address port" works from a command line window and from the start dialog.
Both applications have the same permissions.

Any help appreciated...

Code used:


It's not how you survive the storm, but how you dance in the rain...
Stuart A. Burkett
Ranch Hand

Joined: May 30, 2012
Posts: 679
Lori Cook wrote:
To spawn Remote Desktop, the string passed into the method below is "mstsc.exe /f /v:", "ip_address" set to the IP address of the system and "additional" is empty.
To spawn Telnet, the string passed into the method below is "telnet.exe ", "ip_address" set to the IP address of the system and "additional" is the port desired.

In cases like this you need to look for even the slightest difference. See the text that I've made bold above.
Print the value of prog.toString() and the problem should become clear.
Lori Cook
Greenhorn

Joined: Apr 29, 2013
Posts: 4
Printed out the buffer for the two invocations. The IP address/port removed to protect the innocent. Quotes added around the copy/paste of data.

Telnet invocation: "telnet.exe <address> <port>"
Remote desktop invocation: "mstsc.exe /f /v:<address>"

Took the telnet invocation and pasted into the Search programs and files field of the Start menu and...Got a telnet window/connection.

So...blanks are in the proper place for both invocations. One requires a port, hence the additional field. And either invocation pasted into the Search programs and files field works.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18118
    
  39

Lori Cook wrote:Printed out the buffer for the two invocations. The IP address/port removed to protect the innocent. Quotes added around the copy/paste of data.

Telnet invocation: "telnet.exe <address> <port>"
Remote desktop invocation: "mstsc.exe /f /v:<address>"

Took the telnet invocation and pasted into the Search programs and files field of the Start menu and...Got a telnet window/connection.

So...blanks are in the proper place for both invocations. One requires a port, hence the additional field. And either invocation pasted into the Search programs and files field works.



Perhaps it would be a good idea to use the version of the exec() method that takes a string array, instead of a string? This way, you don't have to worry about the "blanks are in the proper place", or if it is being parsed correctly.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Stuart A. Burkett
Ranch Hand

Joined: May 30, 2012
Posts: 679
Lori Cook wrote:So...blanks are in the proper place for both invocations.

Well your code doesn't add a space between the address and port in your telnet command, so presumably you've included a space either at the end of the ip_address parameter or at the start of the additional parameter.
Relying on the calling code to remember to add that space is always a bad idea. Far better to get your method code to do it or, even better, follow Henry's advice.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7064
    
  16

Lori Cook wrote:Any help appreciated...

Well, I hate to say (because you're probably not going to want to hear it), but running almost anything with Runtime.exec() is likely to be a kludge, because Java was never meant as a vehicle for running platform-based programs. Indeed, it goes against the whole ethos of the language.

If it's a requirement for a course, then obviously you must do it; but if it was me, I'd much rather use a library such as Apache Commons-Net.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Artlicles by Winston can be found here
Lori Cook
Greenhorn

Joined: Apr 29, 2013
Posts: 4
Unfortunately the requirement is to spawn a separate process/application for the user. I might be able to mimic that with the Apache telnet classes, but don't want to re-invent the wheel when an application already exists.
Lori Cook
Greenhorn

Joined: Apr 29, 2013
Posts: 4
Final analysis - you cannot launch telnet within another program, batch file, etc. on Windows 7 (and Vista?). The Windows Security will not let you do it. You have to use some other application or interface to do telnet.

The only time you can run telnet is from within a Windows command window and/or from the Run field of the Startup Menu.
Sigh.

My solution - run a batch file that spawns off a PuTTY telnet session.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Running executables - one works; one doesn't
 
Similar Threads
Which protocol to use?
Problem with Runtime.getRuntime.exec()
My design...getting cloudier
Problem with running a program from Java file
click and go to website