I'm running Java program on solaris and want to redirect both standout and standerr to a log file while still keeping them in the console.
Here's the command I use:
Java -jar MyProgram.jar 2>&1 | tee build.log
However, it gives me the following error:
Ambiguous output redirect
Use ksh or bash, (t)csh doesn't support this syntax (and possibly not even the functionality)
Related
I am new to Java, and installed the following JDK on Windows 10:
C:\Program Files\AdoptOpenJDK\jdk-11.0.8.10-openj9
I can invoke the compiler javac from Cygwin's Bash command line
(in an x-terminal), but it creates many errors that seem to go to
neither stdout or stderr. I need to send them to a file
that I can peruse with Vim.
Here is my invocation command
# mk.bash
# -------
javac \
-classpath "/c/Program Files/.../cplex.jar" \
TestSetup.java
The classpath argument is irrelevant here, as I
only want to focus on capturing javac's output in a Vim-perusable
way.
Issuing ./mk.bash >| mk.out generates an empty mk.out, as does
./mk.bash 2>&1 >| mk.out. I've used the latter pattern for decades
to redirect stderr to stdout and overwrite the destination file.
I can use the script command to send the javac output to mk.out:
script mk.out
./mk.bash
exit
I can then browse the error messages using Vim. However, the
contents are obfuscated by many binary characters (image & link to file below). Normally, I can
clean up messy files with dos2unix, but on this output, it quits due
to binary characters.
As another way to clean up the non-text content, Vim has a
fileformat=dos option which can be entered using :e ++ff=dos %.
The e and % says to edit the current file, while ++ff=dos says
to interpret the file as dos format (ff is fileformat). All
this does is clean up visual artifacts due to the different line
endings in Unix and DOS. All the error messages are still interspersed
with what seem like Escape characters ^[.
Is there any way to get javac to generate only plain text
output or to clean up the output?
Here is an image of the non-plain-text file in Vim:
I doubt it is all that relevant, but I'm following this webpage to
compile a simple Java app TestSetup.java that invokes a 3rd party
tool: https://kunlei.github.io/cplex/cplex-java-setup.
Bottom line up front
The following Bash command sends both stdout and stderr from
javac to both the screen and the file mk.out:
javac -classpath /Some/NonExistent/Jar/File.jar TestSetup.java \
2>&1 | tee mk.out
How it was found
I found that the problem was primarily the use of Bash piping,
redirection, and the script command. The latter is responsible for
the ANSI codes. I could not determine this previously because I could
not capture the javac error messages in a file without script. So
I first solved that problem, then compared the use of script with
the absence of script. Here is what I found.
The following bash command creates a DOS format file without the ANSI
codes:
javac -classpath '/Some/NonExistent/File.jar' TestSetup.java \
>| mk.out 2>&1
Before, I had: (1) 2>&1 >| mk.out instead of (2) >| mk.out 2>&1. For option (1), I thought stderr became directed to
stdout, so I simply needed to direct stdout to mk.out.
But that's not how it works. In option (1), the specification 2>&1
directs stderr to what stdout is currently directed at, i.e., the
screen. The subsequent >| mk.out then directs stdout (and only
std.out) to mk.out. Unfortunately, this meant that stderr was
still going to the screen. Presto, mk.out is left empty (bad).
In option (2), stdout is first directed to mk.out. The subsequent
2>&1 then directs stderr to what stdout is directed to, i.e.,
mk.out. Hence, the javac error messages get sent to mk.out.
It did not contain ANSI codes, which is good.
While option (2) works, it is not ideal; the error messages are not
shown on the screen. This is where I fell back to script and found
that it introduced the ANSI codes:
script \
-c "javac -classpath '/Some/NonExistent/File.jar' TestSetup.java" \
mk.out
Since the above solves the problem of redirecting stderr, I looked
for bash options that avoid script. The following sends stderr to
the screen and to mk.out:
javac -classpath /Some/NonExistent/Jar/File.jar TestSetup.java \
2>&1 | tee mk.out
According to the explanation of option (1) above, it looks like it
shouldn't work, but Bash makes an exception when it is used in a
pipeline. Specifically, stderr is redirected to what stdout is
directed to, but only after all redirections. In a pipeline, the
above means that javac's stdout is directed to tee's stdin,
and only then is javac's stderr directed to the target of
javac's stdout, i.e., tee's stdin.
For reference: Initial solution (javac's Xstdout option)
I found that my javac has a command line option -Xstdout OutputFileName.txt specifically for this purpose.
Unfortunately, not all javac documentation that I found online shows availability of this option. In that case, the most direct solution I found was :term cat The/Output/File.Path in Vim.
Many vim alternatives are also shown on that page, as well as here
For future reference, if needed, 3 fulsome pages that I found to be
relevant are:
https://superuser.com/questions/380772/removing-ansi-color-codes-from-text-stream
https://unix.stackexchange.com/questions/14684/removing-control-chars-including-console-codes-colours-from-script-output
https://unix.stackexchange.com/questions/4527/program-that-passes-stdin-to-stdout-with-color-codes-stripped
Mention is made of ansi2txt, but you need Ubuntu for that.
I am a student of computer science. I am learning java on linux platform ubuntu. While I am compiling a program always I get some compilation error in terminal. I tried to read the error message from terminal. I think reading these error message gives me better understanding about the program/java language itself. So when I recover the error sometimes I would try to read the earlier error message, but the message sometimes removed from the terminal.
So my question is - is there any way to redirecting the error message from terminal to directly to a file.
I am using the fllowing command in ubuntu to compile -
$javac MyClass.java > file_name
But it prints all the line from the terminal to the 'file_name'. Is there any way to redirect only the error message?
Thanks.
You need
$javac MyClass.java 2> file_name
which will redirect stderr. You can (instead) redirect stderr to to stdout and then to a file thus:
$javac MyClass.java 2>&1 > file_name
The above assumes you're using a Bourne-shell type shell (sh/bash/ksh/zsh), as opposed to a C-shell variant.
Having said all that, I would (if at all practical) avoid using the command line for general development in favour of an IDE, which (amongst many other advantages) will tell you a lot more info re. your compilation issues.
I am trying to run my .jar file on a linux server.
These are the commands I'm running:
JAVA_HOME=/app/local/opt/jdk/packages/jdk1.6.0_30/
export PATH=$JAVA_HOME/bin:$PATH
cd folder
nohup java -jar program.jar &
When ran on Friday, this was working perfectly. However, today I am getting:
Unrecognised option: - Could not create the Java virtual machine.
In my nohup.out
If I run java -v after setting the path I get the same problem. Can anyone suggest anything? Really confused about this.
Thanks for any help on this.
Usually, when we get this kind of exception ("Unrecognised option" which causes the "Could not create the Java virtual machine"), the JVM displays the unrecognized option, like this:
Unrecognised option: -[option details] Could not create the Java virtual machine.
the fact that you have no details makes it seem as though you just have a space (" ") in your command.
Take a look again to make sure.
I've got this failure running maven on Win10 where the pom.xml is located in a folder named something like C:\dir\OneDrive - bar\workspace\project.
Moving the project to a different folder solved it.
First of all, there is no -v option on the java command.
(Reference: http://docs.oracle.com/javase/6/docs/technotes/tools/windows/java.html ... Java 6 version ... 'cos that is what you are using. Try java -help for the short list of options.)
So it is expected for java -v to fail.
As an experiment I tried this (with an OpenJDK Java 8 JDK on Fedora 22):
$ java -foop
Unrecognized option: -foop
Error: Could not create the Java Virtual Machine.
Error: A fatal exception has occurred. Program will exit.
$
As you can see, the "unrecognized option" message tells me what option it doesn't understand. Looking at the message in your question, we see that it is saying it is "-". That doesn't appear to match the command line in your question, but on the other hand the command line in your question does not have any invalid options.
Another possibility, I guess, is that you have a shell alias called java. That would take precedence over any commands on the shell's command path.
The other thing is that you are running an ancient version of Java. Java 6 has passed end-of-life, and the last publicly released version is update 45. You are running update 30 which was released in December 2011. There have been LOTS of security patches since then. I'd recommend upgrading to Java 8 ... and making sure that you keep it up to date.
for java 8 run 'java -version'
for java 9 or 9+ run 'java --version'
Had the same issue under Linux/Ubuntu 20.04.
I created a bash to run my jar and used some variables in the file
# This line below shows the error
$JAVA_PATH "-jar $JVM_ARGS $JAR_PATH"
# This line below runs without issue
$JAVA_PATH "-jar" $JVM_ARGS $JAR_PATH $SB_ARGS
I don't know why it works after using double quotes for the "-jar" option only, I think it's something to do with UNIX systems.
How to start .jar application from command line and wrote errors ( exceptionsand warnings, all what is in Console in eclipse during execution ) in errors.text on Windows ?
it think you can do this by starting your jar with the following command:
java -jar myJarFile.jar 2>> errors.txt
the 2>> is used to redirect error messages (System.err)
if you want to redirect normal ouput too use 1> myfile.txt
I invoke a shell command by Process class from java and it prints
"stty: standard input: Invalid argument"
no matter whether the command is right or wrong (normal output of shell command is shown too). If I run the shell command in shell, no such error message is shown.
The command is something like this: {"/bin/csh", "-c", "echo hello"}
You are invoking the stty command from your .profile, or .bash_profile. You'll have to redirect its standard error to /dev/null.
stty blah blah blah 2>/dev/null
stty can't deal with the pseudo-tty that Java provides in shelling out.
Try using the -f option of csh to disable the reading of the .chsrc and .login files:
{"/bin/csh", "-cf", "echo hello"}
Quoth the documentation for java.lang.Process:
"The methods that create processes may
not work well for special processes on
certain native platforms, such as
native windowing processes, daemon
processes, Win16/DOS processes on
Microsoft Windows, or shell scripts.
The created subprocess does not have
its own terminal or console."
Perhaps you would like the java.lang.ProcessBuilder, instead.
Have you look here.
I managed to run some commands like this