Jump to content
  • Advertisement
Sign in to follow this  
unholyx

Java runtime help.

This topic is 2615 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I'm trying to use Java to call a .exe.

There's 3 in total, 2 of them calls correctly but one does not.

Same code for all 3 except the directory.


public void actionPerformed(ActionEvent ae){
if(ae.getSource()==nuConn){
System.out.println("Calling nuConnector1.3...");

try{
Runtime rt = Runtime.getRuntime() ;
Process p = rt.exec("C:\\Users\\Phantom\\Desktop\\nuConnector1.3.exe");
}
catch(Exception e){
}

}


At first I thought it was because the .exe name has a . in it so I changed it to nuConnector13 instead but still doesn't work.

Now the other 2 exe files are actual exe, they open up a client and an actual ui window with options. The nuConnector1.3 is a dos window.

I tried called nuConnector1.3.dos, nuConnector1.3.bat, and nuConnector1.3.cmd (probably not all real extensions but was just my guess).

Whenever opening nuConnector1.3, it has to open up a port which then my OS prompts me to allow it.

Called normally, it would prompt and then open the .exe.
When called through Java, it just prompts and then doesn't open the .exe.

I'm thinking the problem is that it's in a dos window so maybe calling .exe doesn't work :/

Thanks in advance.

Share this post


Link to post
Share on other sites
Advertisement
Well, you're swallowing exceptions silently, so that would be the first place to look.

Assuming that isn't the problem (given you've gotten other processes to start), one issue is that Java automatically redirects the input and output of the process to itself:

The created subprocess does not have its own terminal or console. All its standard io (i.e. stdin, stdout, stderr) operations will be redirected to the parent process through three streams (getOutputStream(), getInputStream(), getErrorStream()). The parent process uses these streams to feed input to and get output from the subprocess. Because some native platforms only provide limited buffer size for standard input and output streams, failure to promptly write the input stream or read the output stream of the subprocess may cause the subprocess to block, and even deadlock.
[/quote]

Thus the console window will not be created unless the called process actually requests one. So the process could actually be running successfully. Take a look in the task manager (or use the much better "process explorer" free tool from Microsoft) and see if you can see it running.

Another issue is that if the process starts running, it is likely going to be blocked on I/O once the pipe is full, because your program doesn't appear to drain the pipe. One option is to start a new background thread, which tries to read from the pipe until the pipe is closed:

class PipeCleaner implements Runnable
{
private final InputStream inputStream;

public PipeCleaner(InputStream inputStream)
{
this.inputStream = inputStream;
}

@Override
public void run()
{
try
{
int i;
while((i = inputStream.read()) != -1)
{
char c = (char)i;
// Ignore the output, or write it to a log somewhere, or whatever
// You might want to use a buffered method of reading
// from the stream if you're not ignoring the data.
doSomethingWith(c);
}
}
catch(IOException e)
{
// Log the exception, or whatever.
}
}
}

// Elsewhere

public void actionPerformed(ActionEvent event)
{
if(event.getSource() == nuConn)
{
System.out.println("Calling nuConnector1.3...");
try
{
Runtime rt = Runtime.getRuntime() ;
Process p = rt.exec("C:\\Users\\Phantom\\Desktop\\nuConnector1.3.exe");
PipeCleaner input = new PipeCleaner(p.getInputStream());
new Thread(input).start();
PipeCleaner error = new PipeCleaner(p.getErrorStream());
new Thread(error).start();
}
catch(Exception e)
{
// Log the error, or display a message box (or both)
}
}

This is obviously a naive implementation, creating two threads to babysit a single process. A better approach might be found by investigating the ProcessBuilder class.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!