سؤال

I'm trying to run a project in Eclipse Indigo where my main class (containing the main method) is in an external .jar file I created from another project. The program is run based upon passing the name of a class in the current project to the main method as a command line argument.

When I try to run this project, the first few lines of the main method (in the .jar) are executed, but then the program exits. The debugger has informed me that it exits with an error code of 1. No exceptions are thrown (or at least, none appear in the console or in my try-catch blocks) and I make no assertion statements of calls to System.exit.

When run directly, the main method complete's normally. This unexpected exit occurs only when the project is exported to a .jar.

I find this exceptionally confusing, especially considering that debugging has confirmed that the main method is called, and simply quits near the end without completing. Here is my main method:

public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, IllegalArgumentException, SecurityException
{   
    Class<?> c = Class.forName(args[0]);

    OpenGL.Renderer r = null;

    Class<? extends OpenGL.Renderer> c2 = c.asSubclass(OpenGL.Renderer.class);

    r = c2.newInstance();

    OpenGL.init(r);
}

The debugger reaches the breakpoint at line 16 (r = c2.newInstance()) before terminating.

EDIT: The problem appears to lie with the reflection code that initializes the variable r. No matter where I put my breakpoints I can't step to a situation in which r is not null.

EDIT #2: After using the debugger some more, I can step into the constructor being called in the c2.newInstance() call, but the program exits immediately after method entry, before any of the constructor is executed. The constructor doesn't contain anything other than a syso call for debugging.

Any help in getting this program to run correctly would be appreciated.

هل كانت مفيدة؟

المحلول

Okay, it's kind of embarrassing, but I finally found the problem, very far away from where my debugger had led to me to believe.

In short, I had a try block that loaded some data from a file and made call to System.exit(1) in the catch block if an exception was thrown. Since the file was located inside the .jar, the path to it was different from what it had been when it located in the src folder in my original project. This caused a FileNotFoundException to be thrown, exiting the program.

For some reason, my debugger reported that the program failed long before it reached this code. For this I continue to have no explanation. If anyone else has this problem, I recommend removing ALL occurrences of System.exit from your code, regardless of where they are, and then continuing to debug.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top