About Java ProcessBuilder's stream - java

first of all.. apologize what i can't upload code..
(thirdparty program is sdelete)
I'm work with third Party program on java ProcessBuilder.
like..
ProcessBuilder("cmd","/C","thirdParty.exe")
or
ProcessBuilder("thirdParty.exe");
and then.. I make two thread that using scanner that get ProcessBuilder's stream.
(one is input Stream print , other one is error Stream print)
When I execute this program.
It seems like fine at opening part. thirdParty program's opening Messages show up in console and .. according to priority.. Process percentage must show up, but doesn't..
Percentage not show up, but it's not hangs or frozzen..
thirdparty work's fine. Just Scanner can't get InputStream data.
(if not with java process, namely if i just execute program.. percentage show up properly)
and.. finally when thirdParty process finished.. all of percentage data show up at once!
is anyone know about this phenomenon?
advice please..

Related

Encountered LineUnavailableException when capturing sound with Java Sound API

I am using Java Sound API to capture sound on a Windows machine by reading data from a TargetDataLine. It works fine if I open a line, read data from the line and then close it. However, If I reopen it once closed, I will get a LineUnavailableException. Can someone explain to me what is going on? If I want to record multiple sound clips, one after another, say repeating this: start -------> record ---------> stop several times, how can I do it?
Thanks
The API says:
Some lines, once closed, cannot be reopened. Attempts to reopen such
a line will always result in a LineUnavailableException.
I think the reason they say "some lines" is that it depends on external factors pertaining to the particular system.
You will need to create a line for each additional recording, it seems.

Exit Java program when user sends EOF (either Ctrl-D or Ctrl-Z)

I want to read radiuses in a loop and exit the program when user sends an EOF (Ctrl+D / Ctrl+Z).
I'm not familiar with Linux and this combination.
Can anyone please explain what an EOF and these commands do? And how should I implement them?
From you program's perspective an EOF is an event when you call a read system call, usually performed by a library, which returns less bytes than requested. There is nothing special about it. It's the same when reading lines from STDIN or a file. No more lines, you exit your main loop.
Linux VTE (virtual terminal) devices arrange this for your program when you hit Ctrl+d.
How to do this in Java, I ain't no clue. I can't even read Java. I never find the needle of meaning in the haystack of boilerplate. :)

Python: Multithreading between Java subproccess and Python listener?

I am monitoring and Minecraft server and I am making a setup file in Python. I need to be able to run two threads, one running the minecraft_server.jar in the console window, while a second thread is constantly checking the output of the minecraft_server. Also, how would I input into the console from Python after starting the Java process?
Example:
thread1 = threading.Thread(target=listener)
thread2 = minecraft_server.jar
def listener():
if minecraft_server.jarOutput == "Server can't keep up!":
sendToTheJavaProccessAsUserInputSomeCommandsToRestartTheServer
It's pretty hard to tell here, but I think what you're asking is how to:
Launch a program in the background.
Send it input, as if it came from a user on the console.
Read its output that it tries to display to a user on the console.
At the same time, run another thread that does other stuff.
The last one is pretty easy; in fact, you've mostly written it, you just need to add a thread1.start() somewhere.
The subprocess module lets you launch a program and control its input and output. It's easiest if you want to just feed in all the input at once, wait until it's done, then process all the output, but obviously that's not your case here, so it's a bit more involved:
minecraft = subprocess.Popen(['java', 'path/to/minecraft_server.jar', '-other', 'args],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
I'm merging stdout and stderr together into one pipe; if you want to read them separately, or send stderr to /dev/null, or whatever, see the docs; it's all pretty simple. While we're making assumptions here, I'm going to assume that minecraft_server uses a simple line-based protocol, where every command, every response, and every info message is exactly one line (that is, under 1K of text ending in a \n).
Now, to send it input, you just do this:
minecraft.stdin.write('Make me a sandwich\n')
Or, in Python 3.x:
minecraft.stdin.write(b'Make me a sandwich\n')
To read its output, you do this:
response = minecraft.stdout.readline()
That works just like a regular file. But note that it works like a binary file. In Python 2.x, the only difference is that newlines don't get automatically converted, but in Python 3.x, it means you can only write bytes (and compatible objects), not strs, and you will receive bytes back. There are good reasons for that, but if you want to get pipes that act like text files instead, see the universal_newlines (and possibly bufsize) arguments under Frequently Used Arguments and Popen Constructor.
Also, it works like a blocking file. With a regular file, this rarely matters, but with a pipe, it's quite possible that there will be data later, but there isn't data yet (because the server hasn't written it yet). So, if there is no output yet (or not a complete line's worth, since I used readline()), your thread just blocks, waiting until there is.
If you don't want that, you probably want to create another thread to service stdout. And its function can actually look pretty similar to what you've got:
def listener():
for line in minecraft.stdout:
if line.strip() == "Server can't keep up!":
minecraft.stdin.write("Restart Universe\n")
Now that thread can block all day and there's no problem, because your other threads are still going.
Well, not quite no problem.
First it's going to be hard to cleanly shut down your program.
More seriously, the pipes between processes have a fixed size; if you don't service stdout fast enough, or the child doesn't service stdin fast enough, the pipe can block. And, the way I've written things, if the stdin pipe blocks, we'll be blocked forever in that stdin.write and won't get to the next read off stdout, so that can block too, and suddenly we're both waiting on each other forever.
You can solve this by having another thread to service stdout. The subprocess module itself includes an example, in the Popen._communicate function used by all the higher-level functions. (Make sure to look at Python 3.3 or later, because earlier versions had bugs.)
If you're in Python 3.4+ (or 3.3 with a backport off PyPI), you can instead use asyncio to rewrite your program around an event loop and handle the input and output the same way you'd write a reactor-based network server. That's what all the cool kids are doing in 2017, but back in late 2014 many people still thought it looked new and scary.
If all of this is sounding like a lot more work than you signed on for, you may want to consider using pexpect, which wraps up a lot of the tedious details, and makes some simplifying assumptions that are probably true in your case.

How to Send a Password to Process in Java

I am launching a process from java to run a command for me. This process runs for a little while, then needs a password to continue. Now I know that I can write to the in stream of the proces, but I am not quite sure how to detect when I need to write to it.
Possible solutions:
Is there a way that I can detect that the process is blocking?
Can I just write to the standard in immediately after executing the command and when the process hits a point when it needs it, it can just read from it?
Any other ideas?
It is not necessary to detect if the child process is blocking or not. If the child process is designed to block until input is provided to it via stdin, it will block until such input is provided.
It it necessary to keep in mind that the standard input, output and error buffer sizes are limited, and therefore it would be necessary for the child process to process the contents of the input buffer, and for the parent process to process the contents of the output and error buffers as soon as possible. Not doing so will result in the child process hanging.
Maybe you should get around the runas problem but not using runas. Google found me this: http://www.source-code.biz/snippets/c/1.htm Lets you pass your password at runtime....

How to invoke main method of java program from a jsp?

I have a JSP from which I'm getting the input from the users. Now, I'd like to pass the input that get from this JSP as arguments to the main method of my java program and run the program from this JSP. Some help please!
Thanks in advance!
Ravilla
Bad idea. What are you trying to achieve? Even if you do that, by calling main() as any other arbitrary method, or by invoking Runtime or something, What you will achieve is a program running on server. What the client would do with that.
JSP/Servlet/Java Web programming is a request/response based stuff.
If you want to compute something using that program and planning to use the output as a response. Then why not include that particular piece as business component or something and call that in a usual way. May be in a separate thread, if its a long process, let the user interact with the application and notify user once it is done.
I had to program something like this a year ago and I ended up making it a webservice. This allowed my app to be called from a JSP. I think you should try this.
you need to execute command at OS level.
Process p = Runtime.getRuntime().exec("java -classpath "..." SomeClassContainingMain ...other arguments);
//you need to consume the outputs of the command if output/error is large otherwise the process is going to hang if output/error buffer is full. and create a seperate thead for it (not created here).
log.debug("PROCESS outputstream : " + p.getInputStream() );
log.debug("PROCESS errorstream : " + p.getErrorStream());
p.waitFor(); // Wait till the process is finished

Categories

Resources