Determine whether the parent process is a terminal - java

I have a C++ program and I want to determine whether the parent process is a terminal or not. Because if it is a terminal, I can use escape codes to highlight the output, else this will be displayed with unreadable characters.
The solution need to run on any posix system.
If anyone knows the solution for Java I would be interested in it as well.

If I understand your problem correctly, you're looking at it from the wrong angle. Ask not what your parent process is, but rather what the capabilities of stdout are. And to do that in a POSIX environment, use isatty().
For stdout, isatty(STDOUT_FILENO) should return 1.
In Java, System.console() will apparently return a Console object if both stdin and stdout are a terminal, and null otherwise. See How can I check if a Java program's input/output streams are connected to a terminal? for more details.

Related

Want to know the exact comment

how to run a jar file in background in windows cmd
this below jar file cmd i want to run in background
java -Djavax.net.ssl.trustStore=cacerts_appedo_agent-Djavax.net.ssl.trustStorePassword=changeit -jar appedo_tomcat_agent_2.0.063.jar
You can't do this in a platform-independent manner.
In Unix/Linux, you would call the fork() system call, which duplicates your process. With Java, that means duplicating the entire JVM. Then, you'd have to figure out whether it's the parent process or the child process, which you can determine from the process ID that the fork() call gives you. If it's the parent process, you exit. If it's the child process, you have to close standard input, standard output and standard error.
In Windows, there appears to be the FreeConsole function, but I know next to nothing about Windows programming.
So conceivably, you could write a JNA library that figures out on which platform you are, and invokes the appropriate calls to achieve this. But it's probably not the best idea to get rid of the console window when starting a Java application.
Use javaw, although you won't get any console output either, which is inconvenient.

Control a Command Prompt Window Like a Swing Window using Java [duplicate]

I would just like to know whether it is possible to make a command prompt in Java.
My friend asked to make it, I wanted to know if it was possible or not. If it is possible, can someone suggest me some api or something? Thank you.
EDIT: I want to make it similar to windows command prompt
EDIT 2: I would like to make a SWING GUI application and put a command prompt inside of it.
Yes. Use the Process API.
You can run commands in Java using the Process API. You can also get the output and write input to the runned process. For more info, see this tutorial.
But if you want to make a terminal emulator (such as those in Linux) in Java,
I recommend having a look at JCTerm or JTA.
You must be careful how you start it.
If you start your program with java.exe then the console (input/output) is shown. With System.out.println("mymessage"); you can print (output) text to the console. With System.in you can read from the console. This delegates to the java.io.Console class (available throug System.console()).
If you start your program with javaw.exe, then you don't see the console. You must then create your own screen to allow input/output. This is the default on Windows.
Java can do console I/O and it can launch processes, so yes, it's possible. You'd use methods of System.in and System.out to display a prompt and read commands, and a ProcessBuilder to execute programs.
yes it's possible in java
your have to do some research on Java.lang & IO
Check the class java.lang.Runtime. It provides a couple of exec() methods.

Why would I want to use java.io.Console over standard streams?

I realize this may be a duplicate question, such as, Why Use java.io.Console?. However, I wanted a more detailed answer (pros/cons, applications, anything more).
Forgive me if I am misunderstanding this. So I ran a program using eclipse and I received the error message "No Console", so I just let it go and ran it via Terminal instead. However, I am a little unclear why is the console object doing this (I think it's because it returns null, but I wanted a more detailed answer). From various reads, it seems various IDEs such as Netbeans and Eclipse haven't "implemented" this properly. So it leads me to the question, and since this is my very first time seeing java.io.Console, why would I want to use this over standard streams? Examples for applications using console is appreciated and preferred!
Programs written with interactive shells in mind provide a lot of conveniences to command line users
tab completion
history tracking
password input secure against shoulder surfers
colored and formatted output
using the preferred pager for paging text
Console allows java programs that are often invoked via the command line to present a good user-experience to users who invoke it in an interactive shell.
For example, Console.readLine might sound like something very similar to BufferedReader.readLine, but when the program is run from an interactive bash shell, it behaves like UNIX readline
DESCRIPTION
readline will read a line from the terminal and return it, using prompt as a prompt. If prompt is NULL or the empty string, no prompt is issued. The line returned is allocated with malloc(3); the caller must free it when finished. The line returned has the final newline removed, so only the text of the line remains.
readline offers editing capabilities while the user is entering the line. By default, the line editing commands are similar to those of emacs. A vi-style line editing interface is also available.
...
SEARCHING
Readline provides commands for searching through the command history for lines containing a specified string. There are two search modes: incremental and non-incremental.
why would I want to use this over standard streams?
If your program produces textual output and its users are likely to include sysadmins who will often invoke it inside an interactive shell, they might benefit from Console.

What is the use of the Console class in java? Is it referring to the java control panel console? Please explain in detail

The java docs says "Methods to access the character-based console device, if any, associated with the current Java virtual machine.".What is the meaning of this sentence?
Roughly: the console = the terminal = the command line.
That is the (usually) black screen that you can use to start your program (with java YourClass), and where you can read parameters from or write to.
The Console object is an easier-to-use alternative to System.in and System.out
In Windows, it would refer to the Command Line (what some people mistakenly refer to as DOS). In *nix systems, it would refer to the Terminal.
The Console class contains methods to handle the command line/terminal, such as reading and writing to it.

Input output communication between two programs

I have a third party java program called kgsgtp.jar which need to communicate with my own C++ (but mainly just C) program. The documentation for the java program states:
=====================
You just need to make sure that stdin for kgsGtp it connected to
the engine's output and stdout for kgsGtp is connected to the engine's
input. Usually, the easiest way to do this is by forking and execing
kgsGtp from within your engine.
=====================
Now I am a reasonably competent programmer and feel that I could probably arrange all that, given just a few more clues. I suspect that if the description was expanded to erm, 10? lines instead of three and a half then I'd have it sorted in no time.
I'm guessing that what the document means by forking, is using WinExec() or CreateProcess() in my program to execute the java program? I'm also guessing that perhaps when I use the right function, then the fact of one program's stdin corresponding to the other's stdout will happen automatically?
That description is for unixes, where a sequence of pipe(),dup2(), fork()/exec() calls would be use to do this.
Take a look at the code snippet in the answer from denis here: How do I get console output in C++ with a Windows program? , should get you started.
Edit: more complete example is here: http://support.microsoft.com/kb/190351
What you need is equivalent of POSIX dup() on windows may be this

Categories