How to view IntelliJ console if program exit code was non-zero? - java

I'm using Intellij IDEA Community 2020.1
When I try to run this super-simple example code:
public class testConsole {
public static void main(String[] args) {
System.out.println("Wish I could see this important info");
System.exit(1);
}
}
I see the important message flash up in the console for a split-second, then the screen immediately switches to an error about the non-zero exit code:
Execution failed for task ':testConsole.main()'.
> Process 'command 'C:/Program Files (x86)/Java/jdk1.7.0_71/bin/java.exe'' finished with non-zero exit value 1
Is there any way to switch back to the console so I can see what was printed to sysout?

I discovered if I click on the top red exclamation point, I can then scroll up to see the sysout.
I think what was happening is to do with having a warning in another part of the project. When I moved that code into a new project that has no compilation errors, the sysout remains even when there's a non-zero exit code.

Related

NetBeans debugger doesn't stop at breakpoint

I'm running a test case in debug mode on a new installation of Netbeans 8.2 with a break point on a myId field.
#Test
public void testCreateDocumentSecurityNullRequest() throws Exception {
final Integer myId = 1;
myRequest request = null;
mockMvc.perform(post("/pathTo/apply/" + myId).contentType(contentType).content(json(request)))
.andExpect(jsonPath("$.code", is("400")));
}
I know the test is executed because I see the test results, but the debugger doesn't stop on my break point.
The Debugger Console shows:
Listening on 23206
User program running
LineBreakpoint myTest.java : 'line #' successfully submitted.
User program finished
I don't understand why it's not stopping and just continuing? I tried it in Eclipse and it stops and let's me continue as I would expect. I'm sure I'm just misunderstanding something simple but I read documentation and nothing sticks out to me. I also looked at some other Stack Overflow posts that were similar but those didn't seem to fit my issue.
I had a similar issue. NetBeans 6.5 won't stop on a breakpoint in my code.
I figured that while working with a project I changed source location (was: "project.6.5", I changed it to just "project").
NetBeans remembered the old source location ("project.6.5") which became non-matching as I ran code from "project". A breakpoint was displayed correctly (red square), but won't stop.
I opened Window > Debug > Sources and checked current code ("project"). Breakpoint became a broken square and it wrote in logs about source not matching.
After I unchecked old source ("project.5.6") it started working fine again.
...\AppData\Roaming\NetBeans\8.2\config\Services\org-netbeans-modules-debugger-Settings.properties - remove all 'breakpoint' lines from this file.

System.out.println terminated

I'm still new to Java and so I'm not understanding where the termination error is coming from. The System.out.println in the main is not being printed out and it says:
< terminated, exit value: 0> C:\Program Files\Java\jre1.8.0_71\bin\javaw.exe (Jan 27, 2016, 1:22:17 PM)
Here is the main of my code.
public class BrainCenter {
public static void main(String[] args) {
System.out.println("Welcome to the Stock Center!");
System.out.println("Realtime Reports within the stock market");
System.out.println("");
Market mk = new Market(6000);
mk.printMarket();
System.out.println("Individual status listed below");
System.out.println("");
Buyers bu = new Buyers();
bu.buildRandomPortfolio(mk);
}
}
The first three lines are not being outputted and I'm not sure why. It appears as the rest of the code loads but then disappear and said to be terminated at the end. I'm using Eclipse IDE.
Any help would be greatly appreciated. Thank you in advance!
Try to use java.exe instead of javaw.exe. The first will wait until the program completes. It will also put all output the program generates directly in console. The last should be used when the program runs with its own GUI or simply in background. It will not wait until the program completes.
See Difference between java.exe and javaw.exe.
you can fix this problem by setting path variable in Environment. In my system I have copied path of bin as "C:\Program Files\Java\jdk1.8.0_25\bin" and saved.

error messages are displaying different in both eclipse and command prompt

Sub: error messages are displaying different in both eclipse and command prompt
//DataHidingDemo program
class Bank {
private static double balance = 1000;// Data Hiding
}
public class DataHidingDemo extends Bank {
public static void main(String[] args) {
System.out.println("Balance:" + balance);
}}
Case 1:
run the program from eclipse and observe below error message is displaying
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The field Bank.balance is not visible
at oops.DataHidingDemo.main(DataHidingDemo.java:10)
Case 2:
run the same program from command prompt and observe below error message is displaying
D:\Java Programs_CMD>javac DataHidingDemo.java
DataHidingDemo.java:10: error: balance has private access in Bank
System.out.println("Balance:"+balance);
^
1 error
Observe both case 1 & 2 error messages; case 2 error message has meaningful.
Q).Do we have the way to display the same error message in eclipse too? (for this do we need to change any setting in eclipse) please help on this.
Eclipse has its own Java compiler, which is thus different from javac, and thus generates different error messages. AFAIK, no, it's not possible to make Eclipse use the javac compiler. Both NetBeans and IntelliJ IDEA use javac, though, so you might want to try thise IDEs instead.
Note that the error you got from Eclipse is a message generated when you tried running code that did not compile. Don't do that. If there are compilation errors listed, then fix them all before running. The compilation errors are listed in the "Problems" view and in the "Markers" view of Eclipse.

How to exit the windows command prompt in which a java application got started

How to exit the console of a simple Java program after displaying an error message?
currently my code has:
...
...
if (some condition){
//print error
System.exit();
...
...
But this System.exit(); leaves the console open. I have tried exit(0);, System.exit(0); as well.
If you're wanting to close the Command Prompt window that your application is running in, then I don't believe there is a way to do it (At least not nicely).
Why do you want to start your application from the command prompt and then close the pre-existing Command Prompt window? This will surely get rid of the error message that you're outputting (unless it's also being logged - in which case why print it to a window you want to close?).
This is Windows specific, but would creating a shortcut in Windows Explorer to java -jar MyJarFile.jar or java MyCompiledClass do what you want? Instructions for this sort of approach can be found here.
System.exit(1);
should work fine. Note that if you're exiting with an error, you would normally set a non-zero exit code. From the doc:
The argument serves as a status code; by convention, a nonzero status
code indicates abnormal termination.
This means you can script using common conventions, any process spawning your program can react correspondingly etc.
If I understand you correctly, you want to run your program in a command prompt and if the program fails you want it to display the error message, close the program AND the commad prompt window?
If this is the case then the only thing I could think of would to be to run your program in a batch file that checks the exit status of your program. So in your code write your error message, then I suggest sleep for a few seconds so the user can actually see it, then exit with status code 1.e.g.
if(SomeCondition){
System.err.println("ERROR MESSAGE...");
Thread.sleep(3000);//Sleep for 3 seconds...
System.exit(1);
}
Then run your program from a batch file which checks the "ERRORLEVEL" environment variable e.g.
java <INSERT_PROGRAM_NAME>
IF %ERRORLEVEL% == 1 exit
Hope this helps :)
java.lang.System doesn't have an exit method with no parameters, so System.exit(); would be a compile-error. (System.exit(1);, however, would be fine.) The reason that your code isn't working is probably that you're not recompiling it, so you're still running some old version from before you added that line.
Suppose you are trying to install firefox .bat file from java
public static void main(String[] args) throws InterruptedException {
try {
String[] command = { "cmd.exe", "/C", "Start", "C:\\firefox.bat" };
Runtime.getRuntime().exec(command).waitFor();
}
catch (Exception e)
{
System.out.println("Execution error");
}
}
This would trigger the command prompt and the window will be opened until you manually go and close it after the firefox is installed.
The fix is that in your .bat file just after your command just put an "exit" For eg:
Your firefox.bat would contain
#Start /wait "Firefox" "C:\Firefox Setup 41.0.exe" -ms
exit
This will close your command prompt window. Hope this helps...
have a look at the below link
http://www.cs.bris.ac.uk/jot/decisions/exercise11/exit.html
There is a general convention that a program must return an exit code. The exit code should be zero to indicate success, and non-zero to indicate failure. Platforms differ about what different non-zero codes mean, so programmers often just return 1 as a general indication of failure.
System.exit(); terminates the JVM. The int is the status code (0 means "normal" exit). If it's not exiting it's because that part of your code is not reachable and not being executed at all.
Java API - System.exit()

System.out.println doesn't work?

When I write this in the main method:
System.out.println("Hello");
Nothing is outputted on the output console. It just says "Build successful (total time: 0 seconds)". What's the problem?
Here is my full program:
package names;
public class myName {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
System.out.println("test");
}
}
Here's the window after I debug the program:
Have no file for /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Classes/jsfd.jar
Have no file for /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Classes/laf.jar
Have no file for /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Classes/sunrsasign.jar
BUILD SUCCESSFUL (total time: 1 second)
On netbeans right click and click run file. It would run. Seems like you're building the project, and not executing it.
Don't debug it. Run it.
I'm nearly sure it's due to an IDE issue: the ant/build output is being redirected to a different place (not stdout) and you're seeing that "other place" as a "Console". Please let us know your IDE and as much code as you can.
You have to run it then. In netbeans, to run you can press F6 - http://netbeans.org/kb/docs/java/quickstart.html

Categories

Resources