daemonize java program - go background after input some data - java

I tried to find a solution for the following use case (on Linux):
Start the program, show some information to the stdout, input some information such as username/password.
The program validate the username/password, then goes to background and run as a daemon.
I did not find a way to do this in Java. There are several sulotions to daemonize java program (such as jsvc, or this: http://barelyenough.org/blog/2005/03/java-daemon/ ). But seems they all do not work for this situation, because the program just goes to background from the beginning, there is no chance to input information before it goes to background.

I don't believe that there's a way to do this purely in java. You could make it work by writing an init script which accepted the command line parameters before spawning the java process in the background. You could use -D command line arguments to pass the user input to the java process.

Related

How do i launch a GUI Terminal and run commands on it in java

I have a program which runs when you double click it. What i want to do is:
Double click a jar File
Launch some GUI
And Run Commands in the terminal through the java program
(just an example, this is not what i want to do)
I Tried this:
public class Main{
public static void main(String [] args){
Runtime.getRuntime().exec("/bin/bash sudo SOME_COMMAND_HERE");
}
}
(SOME_COMMAND_HERE is just a replacement)
It didn't launch a terminal.
(i have the GUI part so no need to write code for that)
So how do i do this? I have been spending a past day or two for just finding answers on stack overflow, and intense googling.
Plus the Reason why i need it is because i want to show the user some progress and entering the password when running sudo commands.
Can some Please help? Thanks in advance.
In your example the process might get stuck as you are not reading it's stdout and stderr streams. From the Process documentation:
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 process may cause the process to block, or even deadlock.
So essentially you are just asking how to execute a process from Java. It is irrelevant that to the user your program would display a terminal-like UI. Knowing this we could rephrase your question and find answers like
Starting a process in Java?
Java Execute a bash script using Java process builder
Execute process from Java irrespective of underlying OS
Java ProcessBuilder - get Output immediately
https://alvinalexander.com/java/java-exec-processbuilder-process-1/

Can someone without JDK run a program that uses the console for i/o (and has no other user interface or display)?

Essentially the question boils down to 'Is there a way to run a class/jar file using the console for input/output without having JDK'. I've written a program in Java for a colleague to use in his work, but he doesn't have/use JDK. I'm aware there are online compilers, but at this point my question has become focused on running a program directly for interest reasons.
Essentially the program is designed to output instructions to the user, the
user makes input in accordance with the instructions, and this goes back and forth for awhile before the program does some calculations based off the input and returns a final result.
I should mention that he has JRE.
My program doesn't use a GUI, it just prints instructions to the console and gathers input from responses typed in the console. It works fine on my computer when running it from the command line ('java myprogram'), but without jdk the java command isn't available to him, which seems to mean he can't run a class file.
My next attempt was to turn the program into an executable jar file, but using the command 'myprogram.jar' from the command line doesn't really do anything. The jar file does include a manifest. I added a blank screen to the code and running the jar file did create the screen, but still no i/o on the console. From what I've read, I think this is because jar files aren't automatically associated with a console, so there is nowhere for the program's output to go or input to come from. He can't use the 'java -jar myprogram.jar' command because he hasn't got the JDK, so even though that command runs the program the way I want it to, it's not an option.
Basically, I'm wondering if it's possible for someone without JDK to run a program and interact with it entirely using the console/command line and no other interface?

Embedding a terminal Window inside of a simple scala application

I'm trying to build a simple text editor using Scala's swing library and I have to support two side by side windows. I was wondering if it was possible to have the second window be the terminal (bash, Unix). I haven't been able to find any information on the subject. Thank you for any information.
The question is: What is "the terminal"? bash is a shell but you need an implementation of a terminal which runs a certain shell. In general, I would say there are two possibilities:
Find a terminal implementation, which can be used directly in Swing. Maybe you this or that may help.
Implement your own terminal. You may want to start to wrap the shell with a ProcessBuilder. Now you can redirect standard input and output of this process so that you can control it programmatically (more information: here and there). Then you have to create the UI part which (1) reads input from the user and (2) displays the shell output in your window.

Java - communicating with a sub app through the input stream

Is there a way I can start a command-line application from java and then send strings (commands) to its input stream and display its response from its output stream?
I'm using an application with a pretty sophisticated command line interface (vlc). The application has an interpreter that responds to a set of commands. For example, after I start the app, I can start or stop a movie by issuing the command 'pause' on the command line.
I'd like to write a java application that executes the program and issues commands to the program. I've seen many examples of java apps starting an application and getting the output stream of the app displaying the output of the app. But I've never seen an example, in which the java app would send requests to the sub-application.
Is there a way I can do this using java?
Thanks in advance!
So long as the spawned process listens on stdin for input, sure.
You'd launch a Process in the usual way (Runtime.exec()) - I won't document it here, as you say you've seen plenty of examples.
Then once you have a handle to the spawned process, you call the confusingly-named getOutputStream. This gives you an OutputStream, the other end of which is connected to the process' standard input. Hence, any bytes written to this stream can be consumed by your child process, just as if you were typing/piping input from a console.
I will point to a couple of resources that are always worth reading when dealing with Processes; cut-and-paste jobs from arbitrary Google results often don't cover the edge cases properly and can lead to deadlocks:
When Runtime.exec() won't (old, but still relevant)
Five common Java Process pitfalls

Permission error while trying to access an (server) program started by a Java program

I am starting a server application (normally to be started from the Unix command line) by using Runtime.getRuntime().exec("path/mmserver"). My problem is now that as long as my Java program, which started that server runs, the server is correctly accessible (from command line and other programs). But when my Java program exits the sever is not accessible anymore (the process of the server is still running). I just get such a error message when trying to access the server: "Error: permission_error(flush_output(user_output),write,stream,user_output,errno(32))".
The server is a blackbox for me.
I am just looking for other ways to start a new process. And maybe someone has a hint why I get that permission error (even if one doesn't know what that server exactly is ... you rather won't know it).
I'm guessing your server program is trying to write to standard output or perhaps standard error (System.out / System.err in Java terms) which it implicitly inherited from your Java program but which turn into pumpkins when your Java program goes away.
A simple solution might be for your Java program to exec a shell script which starts your server as a background process (using START (Windows) or & (Unix)) with explicitly redirected I/O streams.
The Java library has recently gotten some nice updates to the Process class (I think) that allow you to do a lot of fiddling with the streams, but I don't have much experience there so I can't offer a detailed suggestion.
EDIT: My suggestion from the middle paragraph. Untested, sorry!
File server-runner.sh:
#!/bin/bash
/path/mmserver >/dev/null &
You'll need to chmod +x server-runner.sh, of course.
Then, from your Java program, you exec the script server-runner.sh rather than your mmserver.
If you want to kill mmserver, you'll have to find it in ps -ux and use kill on the process number.

Categories

Resources