Java Debugger runs application without breaking - java

I have the ubiquitous HelloWorldApp.java file
/**
* The HelloWorldApp class implements an application that
* simply prints "Hello World!" to standard output.
*/
class HelloWorldApp {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
I run:
javac HelloWorldApp.java
then I run:
jdb HelloWorldApp
I get:
Initializing jdb ...
>
I type:
stop at HelloWorldApp.main:7
where prompted
then I get
Deferring breakpoint HelloWorldApp.main:7.
It will be set after the class is loaded.
>
I type:
run
where prompted
then I get
Set uncaught java.lang.Throwable
Set deferred uncaught java.lang.Throwable
>
VM Started: Hello World!
The application exited
I didn't type anything on that last prompt it just exited without breaking. My question is why did it output those Throwable lines and why didn't the debugger stop at the breakpoint I gave it?

I just checked the syntax for stop in JDB Documentation
stop in <class-name>.<method-name> Stop on entry to the given method.
stop at <class-name>:<line-number> Stop at the given line.
I think you command for stop should be either one of the following
stop in HelloWorldApp.main
stop at HelloWorldApp:7
Give it a try to see that fixes your issue!

Related

ar.readLine not reading a input from user?

Summary
1. I am import java.io.console,i am not getting any error in my code,
code is correct, I thought, It's getting some error in eclipse
software... Error :
Exception in thread "main" java.lang.NullPointerException at
loops.Escapey.main(Escapey.java:9)
Coding:
package loops;
import java.io.Console;
public class Escapey {
public static void main(String[] args) {
Console ar = System.console(); // creating a new object for console
String name = ar.readLine("how old are you ?"); //reads a user input
System.out.printf("%s - pretty age",name);
}
}
Ouput :
- I except the output how old are you ?? 18 18 - pretty age
But the actual output is getting error... Exception in thread "main"
java.lang.NullPointerException at loops.Escapey.main(Escapey.java:9)
I assume you are running this into some IDE. Since System.console returns the attached console, if you run it into IDE, it will return NULL. The better approach is to use Scanner class. If you really want to use System.console, you will have to test it on some console. If on Mac, run the terminal. On Linux any of the terminal apps would work.
If we run this in IDE (Intellij), it is throwing null pointer exception. Since this is related to console, I tried to execute this in command line and it works fine.
Execute these steps in terminal or command line and it will work
1) javac Escapey.java
2) java Escapey

Specify Java command line options after jar execution

In an effort to make my app more OSX friendly, I am trying to set the dock name of my program to something like MyApp instead of a fully qualified class name (the default), such as myproject.mypackage.mysubpackage.myclass. Obviously, the first is much more appealing.
To do this, I use the OSX -Xdock:name command as a command line option when executing my .jar file. So to execute it, the command might look something like java -Xdock:name=MyApp -jar /mypath/myjar.jar. This works perfectly and sets the .jar's dock name to MyApp. But the issue is that this .jar will never be executed via command line and will be a double-clicked runnable .jar with a GUI display.
The only way I have thought of to set this command line option programmatically is to have a second class execute the class that actually starts the program. So something like this:
public class AppStarter {
public static void main(String[] args) {
String cmd = "java -Xdock:name=MyApp -cp myproject/mypackage/AppBuilder";
try {
Runtime runtime = Runtime.getRuntime();
runtime.exec(cmd);
} catch(IOException ex) {
//Display error message
}
}
}
public class AppBuilder {
public static void main(String[] args) {
//Start actual program and build GUI display
}
}
So here, AppStarter sets the command line options for AppBuilder, which when executed, has the dock name MyApp. The problem I see with this is that it is very tightly coupled. If for some reason the command line is inaccessible on the device or some IOException keeps getting thrown, literally nothing will happen with the program and it will be dead. There would be no way for the average computer user to recover from this.
So I'm wondering if it is possible to perhaps set these command line options after the .jar has already started executing. The old way to programmatically set the app's name has been ineffective for several OSX updates, so I'm stuck with only this command line option. Thanks for any advice.
Once the java command is executed, the command line arguments are parsed and set for the running JVM. You cannot change it any more.
This is usually handled by execution scripts (bash, etc.). If you cannot use them, you can use your approach, but the biggest disadvantage is that it will be running in a separate process.

Terminated nonerror in eclipse java regarding javaw.exe

I realize that when eclipse says:
<terminated> main [java application] C:\program files\java\jre1.8.0_25\bin\javaw.exe
That the program ran and terminated. My problem is that it doesn't really run, as soon as I hit run it immediately shows this message. The program doesn't run at all. Anyone know how to stop javaw.exe from terminating the program immediately?
public class main {
public static void main(final String[] args) throws Exception {
Twitchbot bot = new Twitchbot();
bot.setVerbose(true);
bot.connect("irc.twitch.tv", "6667", "oauth:*********************************");
bot.joinChannel("#donnie64");
}
}
That's because your program did run, and It did exit, all without error or output. It creates a Twitchbot, sets it to verbose, connects, joins the channel, then the program ends after that because it has no more instructions, loops, or threads to wait for. The only thing it says is
<terminated> main [java application] C:\program files\java\jre1.8.0_25\bin\javaw.exe
because that's what Eclipse is supposed to say when a program ends.
If there was an error, it should've printed something in the console in red letters like:
Exception in thread "main" java.lang.RuntimeException
at testing.NFATest.main(NFATest.java:45)
or whatever exception it threw. (NullPointerException, ArrayIndexOutOfBounds, etc)

jdb print caught exception message

I have exceptions thrown in my java code when an error occurs. I then run the code using jdb so when the exception occurs, I can see the state that the code is in and debug. For all exceptions that I throw, I put in a useful string message. However, when jdb catches the exception, it doesn't print this string along with it. How do I print this string?
I have googled and searched and read the documentation but I can't figure out how.
If I have the test class:
public class Test{
public static void main(String[] args){
throw new IllegalArgumentException("How do I view this string through jdb?");
}
}
And run it through jdb:
$ jdb Test
Initializing jdb ...
> run
run Test
Set uncaught java.lang.Throwable
Set deferred uncaught java.lang.Throwable
>
VM Started:
Exception occurred: java.lang.IllegalArgumentException (uncaught)"thread=main", Test.main(), line=3 bci=9
main[1]
Check out the JDB "catch" command, for example "catch IllegalArgumentException". Per the JDB docs, it will cause the debugger to break when the exception is thrown so that you can probe into the details. This document on debugging does a nice job explaining some of the downsides to being dependant on "print" statements, and also references the JDB "catch" functionality.

Getting exit value of a shell script 255 every time

I have a jar say test.jar having TestJar as its main class, a shell script jar_executor.sh,and a java file. My test.jar will return 1 if we pass 1 as an argument and 2 if we pass any other value.
My shell script is executing test.jar as follow
#!/usr/bin/ksh
java TestJar 1 -cp test.jar
return_code=$?
exit $return_code
In java file I am creating a process and executing shell script and taking its exitvalue with following code
Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec(sh jar_executor.sh);
int exitVal = process.waitFor();
System.out.println(exitVal);
This exitVal variable should print 1 or 2 as per argument we are passing but it is printing 255 everytime.
If I use echo $return_code in shell script then I am getting correct value.
Please help me why I am getting value 255 with exit. Thanks in advance!!!
255 or -1 is an application defined exit code, you would have to read the code to know what it means.
A Java application which exits normally, returns 0 and if it throws an Exception, returns 1.
Go to your workspace library, workspace.metadata.plugins\org.eclipse.e4.workbench
and remove the workbench file :) . after that you can restart eclipse
It looks like it might be a Java bug. Others have reported issues similar to what you've seen; see this Java bug report. The Java devs don't think there's a bug but I'm suspicious. The code JRE uses to get the return value from a spawned process is hairy, and I wouldn't be surprised if there's a race condition or other concurrency bug.
I'm guessing that the JRE fails to capture the return code if the spawned process exits very quickly. If my suspicion is correct, adding sleep 1 to your shell script will cause it to work.
Your shell script does not call java correctly:
java TestJar 1 -cp test.jar
You need to set the options for the java command before you mention your main class name. All arguments after your main class name are arguments for your main class and are no longer options for the java VM. So in your case there is no classpath specified for java. I get the following error message when I execute your script: Error: Could not find or load main class TestJar. To fix, you have just to re-order the arguments in your jar_executor.sh script:
java -cp test.jar TestJar 1
I cannot reproduce the 255 on my PC, so I can only guess where that comes from: Either your java command returns an error code of 255 instead of 1 when it fails to load the main class or your korn shell (/usr/bin/ksh) sets this return value when the script is aborted.
Here are all sources I used:
jar_executor.sh
#!/bin/sh -e
java -cp test.jar TestJar 2
return_code=$?
exit $return_code
TestJar.java
public class TestJar {
public static void main(final String[] args) {
System.exit(Integer.parseInt(args[0]));
}
}
JarRunner.java
import java.io.IOException;
public class JarRunner {
public static void main(final String[] args) throws IOException, InterruptedException {
final Runtime runtime = Runtime.getRuntime();
final Process process = runtime.exec("sh jar_executor.sh");
final int exitVal = process.waitFor();
System.out.println(exitVal);
}
}
When I now run java -cp bin JarRunner, I get the output 2 as expected.
You're probably wrong in invoking the script. Try:
Process process = runtime.exec("sh -c jar_executor.sh");
Note the "-c" flag that means you're calling the shell to execute the command.

Categories