procrun in --StartMode=Jvm running main that throws exception does not terminate - java

Trying out Apache procrun I see a behaviour that I do not understand. It boils down to the main method of the Java program throwing an exception. This is not logged anywhere and the Windows service does not stop. To investigate further I changed the main method to
public static void main(String[] args) throws Exception {
if (args.length<10000000) {
throw new Exception("one exception right away");
}
...
}
When I install this as a service with prunsrv.exe and then start it, it starts without problems and produces no log output whatsoever. In particular the service does not stop.
For reference, here is how the service is installed with procrun:
& $procrun "//$operation//$service" `
--DisplayName="$service" `
--Description="$service" `
--DependsOn="$depends" `
--Startup=auto `
--Install="$procrun" `
--Jvm="$JVM" `
--Classpath="$cp" `
--Environment="PATH=$env:JAVA_HOME\bin" `
--JavaHome="$env:JAVA_HOME" `
--StartPath="c:\Search" `
--JvmOptions="-Xmx512M;-Xms512M;-Djava.awt.headless=true" `
--StartMode="jvm" `
--StartClass="$classname" `
--LogPath="c:\Search\std-logs" `
--LogPrefix="procrun-$service" `
--LogLevel="Debug" `
--StdError="c:\Search\std-logs\stderr-$no0" `
--StdOutput="c:\Search\std-logs\stdout-$no0" `
--StartParams="(unused)"
I would have expected that the service stops right away and I find some log output in the stderr/stdout files, but nothing. Can anyone explain this?
EDIT: Oh my, it seems like procrun swallows java.lang.Error (in my case java.lang.noClassDefFoundError) instead of screaming, shouting and exiting.

After I wrapped a catch(Throwable) around the main, logging whatever popped out and then exiting without re-throwing, I saw the problem. I got a java.lang.NoClassDefFoundError.
Bottom line: procrun seems to swallow error type exceptions and does not even terminate if one is thrown in the called Java program.
Created an issue: https://issues.apache.org/jira/browse/DAEMON-344

Related

java.lang.NoSuchMethodError - Ljava/lang/String;)Ljava/lang/String;

My code is giving an error below;
Exception in thread "main" java.lang.NoSuchMethodError:
com/myApp/Client.cypherCBC(Ljava/lang/String;)Ljava/lang/String;
But it's working fine in an another local environment. My code so far is below;
try {
System.out.println("Encrypted CBC passwd : "
+ Client.cypherCBC("CypherThePassword"));
} catch (Exception e) {
e.printStackTrace();
}
This is due to a run-time JAR or class mismatch. the "Client" class which was there at the time you compile your application has a static method "cypherCBC" which gets String parameter, but at run-time class loader has loaded the "Client" class which doesn't have that kind of method (same name with same signature).
if you can debug the application at runtime, put a break-point at the line which exception was thrown, then try to evaluate following expression,
Client.class.getResource("Client.class")
, then you can find where the class has been leaded from, then you can decompile and try to troubleshoot the issue.
I got the same error while running a web application in Weblogic.
The reason for this error is there are two versions of classes are in the environment.To fix this you have to figure out which .class is using at the run time.
I used following to identify which class is loaded at run time.
-verbose:class
There is a duplicate class on your classpath.
So, That is why JVM is getting confused that which one needs to pick because both classes have a same method with a different signature that you are trying to call.

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)

Error in sphinx helloworld code

I am trying to set up sphinx, i am following this tutorial.
When i copy and paste the code of hello world from "sphinx4-0.1alpha-src"; i get an error on line:
} catch (InstantiationException e) {
Error says Unreachable catch block for InstantiationException.
This exception is never thrown from the try statement body.
I mean i have not written this code, and even in the video there is this line but no error comes up. I know that it is not a big deal and if i run the program and click avoid the error, it runs properly.
[I am using Eclipse "eclipse-standard-kepler-SR1-win32-x86_64" on a windows 8 machine]
List of files in that folder:
build.xml
hello.gram
helloworld.config.xml
HelloWorld.java
helloworld.Manifest
README.html
Ques - why do i see this error at all?
(I think there is not a problem in the code as it is alpha edition of sphnix)
It could be because this exception is never declared to be thrown from your try{} block, just remove last catch block

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.

Capturing a java NoClassDefFoundError thrown by program called via shell script

I have a java program which is a compiler and executor for Jasper Reports and is called via shell script on our reports server.
I just fixed a bug caused by a missing dependancy, but it took me a while to figure out what was going wrong. Normally, problems caused by the compile process are captured, recorded to a log file and emailed to the relevant person. As this was a NoClassDefFoundError, it basically exited the program and failed in a silent manner from the users perspective.
Is there any way for me to capture errors like this one so that they can also be emailed away? I have the authority to modify the executing shell script.
Typically errors are not caught by application code and are thrown to JVM level where they are printed to STDERR. So, your way to track this error is to redirect STDERR to file:
java -cp YourMain 1>stdout.log 2>stderr.log
You can also put both STDOUT and STDERR together:
java -cp YourMain 1>&2 2>wholelog.log
There is a lot of reference about stream redirection in web. You can take a look there if my examples do not satisfy you. And it a depends on your OS.
Just can catch the error i.e.
try {
numericDefinition = new net.sf.cb2xml.def.BasicNumericDefinition(
binName, binarySizes, SynchronizeAt, usePositive, floatSynchronize, doubleSynchronize
);
} catch (NoClassDefFoundError e) {
System.out.println("Class Not Found: " + e.getMessage());
}
You do need to be very careful of your coding though, it is easy to get NoClassDefFoundError thrown at class initialisation time and not get into to the try .. catch block.
The NoClassDefFoundError will be thrown the first time a class is refereneced which could be when could when a class uses a class which uses a class which uses a class ... which uses a class that references a class that does not exist.
The following may fail with NoClassDefFoundError at class initialization because of the import.
import net.sf.cb2xml.def.BasicNumericDefinition; // could cause the NoClassDefFoundError
...........
try {
numericDefinition = new BasicNumericDefinition(
binName, binarySizes, SynchronizeAt, usePositive, floatSynchronize, doubleSynchronize
);
} catch (NoClassDefFoundError e) {
System.out.println("Class Not Found: " + e.getMessage());
}

Categories