I have a peculiar problem. I have a Java program that is run with the command :
cat input_file_name | ./script_name > file_output_name
the script_name script just does : java myprogram
My question is : how can I print out something in the console without it being "put in the file_output_name file" (since the > file puts all System.out.prints in that file)
I know this is possible because there are some already that come from some class in a library that I'm using from my java program. However I can't find the exact source of those prints so I don't know how it is coded.
Thank you for any help.
The simple answer is to write those messages to System.err rather than System.out.
This will work since > redirects standard output but not standard error. (You can redirect standard error, but the shell syntax is different; e.g. foo 2> /tmp/somefile.)
A more complicated alternative is to change your program so that it can be called with the name of the output file as an argument. Then open the file, wrap it in a PrintWriter and write to that stream rather than System.out. With a bit of refactoring, you should be able to structure your program so that it can be used either way.
The easiest way is to use System.err.println() instead of System.out.
It will go to a different "device" (stderr instead of stdout), and it won't be redirected.
With what you've shown, you're only redirecting the standard out (stdout). You can write something to the standard error (stderr) instead to have it show on the console yet. In Java, this can be done by System.err.println(...), and related methods.
Notice that if a Java program writes to standard output, it can't control where does its output get redirected, that depends on how the program is invoked from the command line.
Having that clear, you can redirect the program's standard output only to the console, by simply removing the > file_output_name part from the command.
Or, you can redirect the output of the program to both the console and a file by using the tee command, take a look at this article which explains in detail how to do it.
Related
In a Clojure program, how do you read from standard out? I want to do that, or pipe the standard output, to an input stream that I create. The standard output in Clojure is a java.io.PrintWriter .
I have a Samza job, started by a Clojure program. There's also an nrepl server to which I can remotely connect. After connecting, I need to be able to tap into and tail standard out (to which jobs write their output).
1) As per this SO question, with-out-str (see here) lets us temporarily bind *out* (to a java.io.StringWriter), so that your executed code writes to a string. But that doesn't let me tap into the existing *out*.
2) If you look at clojure.java.shell (see here), it gets the JVM's Runtime and exec's a Process on it. From that process, you can get its standard output stream. But again, that's not the default standard out (*out*) I'm looking for.
3) This SO question gets close to what I'm trying to do. But again, I'm connecting to an existing process, and want to tail out its standard output.
Is this possible in Clojure (see here)? Has anyone solved this?
Process output is not a publish subscribe model, so in effect when a process puts a character into it's output buffer, exactly one process gets to pull it off that buffer. If you have a program that was started by a shell that shell process if reading it's output and writing it to a terminal (or reading and ignoring it). If you attach your process after the process that started it and start trying to grab the data, you will most likely not get anything because the parent process will get it first. I just tried this from two terminals:
Terminal 1:
cat
Terminal 2:
ps -ef | grep cat
tail -f /proc/24547/fd/2
Terminal 1:
hello
Terminal 2:
< nothing >
The string "hello" printed to terminal 1, the process that started it.
It's tempting then to say "well what if nobody reads the output, then it will be there for me to get". While this sounds good it runs into the problem that these are fixed sized buffers, so as soon as the output buffer is full the process that is trying to write to it blocks (is prevented from running at all) until someone reads the output to unblock it.
The general solution is to pipe the process you want to tail later to the tee command which writes the output to a file and passes it to whatever was reading it.
command-to-watch arg1 arg2 | tee logfile.potentially-huge
Though if you go this route you should rotate the log file before your disk fills. Make sure you empty the log file with exactly this command
echo > logfile.potentially-huge
or use your program to make a truncate call to the file. simply deleting the file will remove it's name from the log directory without deleting it, it will silently continue to grow taking up disk space and the new file will get no output ever.
This is basically why we built log libraries like log4j (in the 90s) and syslog (in the 80s).
If you still want to get hackish crazy on this, turn to tmux, it can do anything, and changes the way people work with text. In all seriousness you should change the way the other process creates it's output to make it easier to get.
I needed to execute java class third party from shell script. When exception occurs, they are writing error message with System.err as per requirement. Now, when i executed the class in shell, i want to get that error message. I cannot get complete message in standard output and search for this message as its a big script and there are lot of messages. Is there a way to capture that specific System.err printed in java when i executed it in shell. ?Just let me know, if the problem is not clear enough.
To pick your java application (or whatever program for that matter) standard error in a variable while discarding its standard output, you can use this shell construction:
var=$(java MyApplication 2>&1 1>/dev/null)
If you don't want to discard the standard output, you can redirect it to a file instead of /dev/null, or redirect it to your terminal emulator using /dev/tty.
Please refer this-
How to print an error message to standard error?
I think the best answer to that question answers your question as well.
We are building a grading system, and part of its job is to take input files, and from the given directory, our system will compile and run a (non-java) source code file written by the students. Then, the system will display the output from that run.
It is not limited to python, any other language that can meet the requirement is OK.
Is there is any method for providing for the location of the python code and the input file, and then run that code, returning the output file. If not, how can i achieve this goal?
Take a look at the Runtime class. In particular, look at Runtime.exec(). It should enable you to execute external applications from within your Java program, as well as passing command line arguments, and specifying working directory.
Note that the python or any other program must have some well-defined way to get its input and write its output, such as passing filenames on the command line, or reading from stdin.
In UNIX, I'm supposed to write a Java file that will print "EXIT 1" to the standard error, and then exit with a status of 1.
Here is my approach..
System.err.println("EXIT 1");
System.exit(1);
Is this what I'm supposed to do?
If so, how am I supposed to use it in the Unix shells? When I compile and run it in the bash, it just prints "EXIT 1" (so it does the same thing as System.out.println, why should I use "err"?). What is the "standard error" here?
Every running program has these three streams:
Standard input (stdin), which normally comes from the keyboard. Exposed as System.in
Standard out (stdout), which normally goes to the console. Exposed as
System.out
Standard error (stderr), which normally also goes to the console. Exposed as System.err
Your program is correct – it does print to stderr. But under normal circumstances, the stderr stream goes to the console just like the stdout stream, so they are visually indistinguishable.
However, the reason you should use stderr instead of stdout for error messages, is redirection. That means that you send stderr to a file instead of the console. Meanwhile, stdout will be unaffected, because the two streams are independent.
For example, you can do this in bash, cmd, PowerShell, etc:
$ java Program 2> errors.txt
Now, all output with System.err.println() will end up in errors.txt, while System.out.println() will still go to the screen. This can help with debugging.
There are three data streams associated with nearly every process:
Standard Input: This is the stream of input into a program, either from a terminal, a console, piped output from another process, or some other means.
Standard Error: This is where all debugging and error messages should go. This is so that this sort of information can easily be separately captured from the regular output of a program. Web servers do this, by sending error messages to an error_log file via stderr, while the normal log file would be e. g. access_log.
Standard Output: This is the where all typical, expected output that a user running a program should expect to see said output appear.
Standard Output (stdout) and Standard Error (stderr) are nearly always the first and second output streams coming from a process, respectively. This allows me to do something like /path/to/my/neat/program > logs/program.log 2> logs/program.err and have output and errors nicely sorted.
i want to read the data from the batch file to java program. my java program is calling the batch file & it is giving the output for following command
C:>FIND "check" d:\c.txt
---------- D:\C.TXT
check
i want to read this "check" in my java program.
Thanks,
Murali
You haven't shown us how do you execute this batch. If you use Runtime.exec() then have a look at this example of capturing output: http://www.rgagnon.com/javadetails/java-0014.html
You can redirect the output of the FIND command to a dynamically named file and have it read by Java.
C:\FIND "check" d:\c.txt > yourUniqueFileName.txt
Then read the file yourUniqueFileName.text, parse and delete on the end (or not).
You can use ProcessBuilder or Process to exec() and then capture the output. More info here. You could redirect to a file and then read the file - however you're at the mercy of diskspace/permissioning issues, plus you should uniquely name your file etc.
Note that you'll need to be careful when capturing output from a spawned process. See this answer for more details.