Printing the compilation errors to a file in disk - java

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.

Related

hsdis Windows Compilation Failure - C compiler cannot create executables - Error 77, Error 2

I am attempting to build hsdis for use on Windows. I first tried using Cygwin via the following command:
make ARCH=amd64 BINUTILS=binutils-2.26 MINGW=x86_64-w64-mingw32
And receive this log file with errors beginning at line 103.
I then attempted to use the MinGW64 cross-compiler (as outlined here) on a Linux VM with the following command:
make ARCH=amd64 MINGW=x86-64-w64-mingw32 BINUTILS=binutils-2.26
However, the result was this log file and no built DLL.
Any advice would be greatly appreciated, and I would be more than happy to provide any additional information. Thank you!

Java compilation through command prompt issue

I tried to compile my class file "checker.class" into "checker.java" through the use of the javac command, planning to run it with the java command, but javac either gave me the issue of "file not found" OR "invalid flag" (when I tried to put checker in quotations.)
I would also like to understand the issue itself better and not just know the solution. Thanks.
What I attempted:
C:\Users\jaede\Desktop\everything>javac checker.class
C:\Users\jaede\Desktop\everything>javac "checker.class"
Results with error messages:
error: file not found: Checker.java
Usage: javac
use --help for a list of possible options
error: invalid flag: Checker.class Usage: javac
use --help for a list of possible options
planned to do:
C:\Users\jaede\Desktop\everything>java checker.java
*Sidenote: I probably do not have an environment variable issue, since I could run this with another .java files with the java command
I got .class and .java mixed up. The comments to the original post have the full rundown.

Ambiguous output redirect on solaris

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)

Starting an external Java program from a Java program

This question has been asked before but with no real answer.
I wan't to start a Java Progam form another Java Program.
In my case I wan't to start the same program(2) and then exit the original program(1) while the clone is still open.
Unfortunatly I can't get this to work with ProcessBuilder
ProcessBuilder processBuilder = new ProcessBuilder("java","Programm");
Process process = processBuilder.start();
No error message, nothing happens.
I also can't run the program from the CMD in windows.
javac shows several errors during compilation:
symbol not found, ...
I can start the program normally from Eclipse.
You probably aren't seeing the errors from your Process because they are sent to its own error stream. You can access the error stream using process.getErrorStream().
I suspect the problem is that your classpath isn't properly set when invoking the java executable and it is failing to find your class or its dependencies, but it is hard to tell without seeing the error.

SQLCMD not working when run using Java

This is an extremely strange situation, but I just cannot point out what I'm doing wrong.
I'm executing a big bunch of SQL scripts (table creation scripts, mostly). They are executed through Java, using sqlcmd. Here's the sqlcmd command I use.
sqlcmd -m 11 -S SERVER -d DB -U USER -P PASS -r0 -i "SCRIPT.sql" 2> "ERRORS.log" 1> NULL
Note: I use the -r0 and redirects to make sure only errors go into the log file. I chuck out all STDOUTs.
Now I execute this command in Java, using getRuntime.exec(), like this.
Runtime.getRuntime().gc();
strCmd = "cmd /c sqlcmd -m 11 -S SERVER -d DB -U USER -P PASS -r0 -i \"SCRIPT.sql\" 2> \"ERRORS.log\" 1> NULL"
Process proc = Runtime.getRuntime().exec(strCmd);
proc.waitFor();
Note: I use cmd /c, so that the command runs in its own shell and exits gracefully. Also, this helps in immediately reading the error log to look for errors.
The Problem!
This command works perfectly when run by hand on the command prompt (i.e. the tables are getting created as intended). However, when executed through Java as shown, the scripts are run, and and there are no errors, no exceptions, nothing in the logs. But, when checking in SSMS, the tables aren't there!
Where do I even begin debugging this issue?
UPDATE: I'M A MORON
The return value from the getRuntime().exec method is 1. It should be 0, which denotes normal execution.
Any pointers on how to fix this?
UPDATE 2
I've looked at the process' ErrorStream, and this is what it has.
Sqlcmd: Error: Error occurred while opening or operating on file 2>
(Reason: The filename, directory name, or volume label syntax is
incorrect).
Looks like the path I'm passing is wrong. The error log goes into my profile directory, which is C:\Documents and Settings\my_username. Do the spaces in the path matter? I'm anyways double-quoting them!
Have a look at the exec method with an string array as parameter:
java.lang.Runtime.exec(String[] cmdArray)
The JavaDoc for this method says:
Executes the specified command and arguments in a separate process.
So, the first item in the array is the command and all of your arguments are appended to the array, e. g.,
Runtime.getRuntime().exec(new String[] {"cmd", "/c", "sqlcmd ... "});
After looking at your comment and the implementation of exec(String) it seems to be, that the exec method recognizes the pipe operator > as an argument to cmd, because exec(String) splits the command string to an array using whitespaces as seperators.
I don't have privs to post comments - which is what this is - but what if you try putting in a bogus user id for the DB? Does that cause a different execution path? Will that give you a Java error? Or an Auth error in your DB? Also, def tweak the user, not the password and learn from my experience that if you tweak the password that's a great way to get an account locked out!
The other thing - and this may be a shot in the dark - but what are the JRE and driver you're using? I believe there's a known issue with JRE 1.6.0.29 and the sqljdbc4 JAR. I have more details on this, but I'll have to post the link once I get to work.
Edit:
I know it's been established that the JRE/sqljdbc combo isn't your issue, but if folks search and find this, here is the link I spoke of above:
Driver.getConnection hangs using SQLServer driver and Java 1.6.0_29
First enable log/view commands output (since exec() returns 1), which would point out possible cause of the issue.
Use proc.getInputStream() and print the contents to a file or console.

Categories