I figured it out!
The way I found the problem:
After double, triple checking the dependencies and seeing that it both compiled and then built fine, I suspected the jar file which is located at build/deploy/{yourProjectName}.jar
EDIT: If you're getting the errors only once it is installed, it makes sense to run the jar in the installation directory. ( AppData/Local/{ApplicationTitle}/app/{yourProjectName}.jar
)
I ran it on the command line to see if it was throwing an exception:
To easily navigate to the directory I noted where I had saved my project in Explorer and then copied that to the command line.
Open command prompt
Win + r
cmd
+ Enter
Navigate to the directory
cd {ProjectPath}\build\deploy
Run the jar file
java -jar "{YourJar}.jar"
Because I executed the jar via the command prompt, java had somewhere to show me the exception!
C:\Users\administrator.SUNDANCE\IdeaProjects\PODTester_Layout8\build\deploy>java
-jar "PODTester_Layout8.jar"
java.io.FileNotFoundException: src\resources\Carrier List.txt (The system cannot
find the path specified)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileReader.<init>(Unknown Source)
Upon reviewing my code, I figured that it wasn't finding the file because of the explicit reference to src
. I fixed that and lo and behold! Upon rebuilding, the installed application worked!
So in summary:
If your native package throws you a
Error invoking method.
and a
Failed to launch JVM
run your jar file via the command prompt to see if it throws exceptions.
Now if you want details about my specific problem and the process I went through to fix it, here you go:
The Problem:
I thought it might've been something to do with dependencies (especially with me including the sqljdbc driver), but it turns out it wasn't a dependency issue. The problem was that the .jar was throwing an exception only once installed. What made the error so hard to understand was the oh-so-generic "Could not invoke method." which gave me little new information.
To mitigate future similar problems I added a showExceptionDialog()
method (inspired by Marco Jacob's work) :
/**
* Shows a dialog box when an exception occurs instead of just doing nothing.
*
* Once installed this will help to diagnose problems instead of letting
* them go unnoticed.
*
* @param e the exception to print; it's stacktrace will be shown as well
*/
public static void showExceptionDialog(Exception e) {
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setTitle("Exception Dialog");
alert.setHeaderText("An error occurred:");
String content = "Error: ";
if (null != e) {
content += e.toString() + "\n\n";
}
alert.setContentText(content);
Exception ex = new Exception(e);
//Create expandable Exception.
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
ex.printStackTrace(pw);
String exceptionText = sw.toString();
//Set up TextArea
TextArea textArea = new TextArea(exceptionText);
textArea.setEditable(false);
textArea.setWrapText(true);
textArea.setPrefHeight(600);
textArea.setPrefWidth(800);
//Set expandable Exception into the dialog pane.
alert.getDialogPane().setExpandableContent(textArea);
alert.showAndWait();
}
Specific details about my problem:
In my program I had a file called Carrier List.txt
being read and written to, and it was stored in the resources
folder.
In IntelliJ and Eclipse it compiled just fine because it could find src/resources/Carrier List.txt
just fine. Things would have been just fine when I manually copied the resources folder as per Marco Jacob's Deployment Tutorial, but I explicitly referenced
src/resources/Carrier List.txt
in my code instead of just
resources/Carrier List.txt
Best Answer
I ran into the same problem; the following worked for me and helped me make sense of those blasted "Error invoking method." and "Failed to launch JVM" dialogs:
.jar
fileAppData\Local\{ApplicationTitle}\app
(shortcut: type %appdata% into explorer); if your project was named HelloWorld, there you will findHelloWorld.jar
cd
to the same directory using the command prompt).jar
via the command linejava -jar "HelloWorld.jar"
and hit EnterTadah! Behold your hidden exceptions (the existence of which "Error invoking method." so vaguely tries to communicate to you). *
If your problem is similar to mine it stems from a file structure difference between the project
out
folder and the installation directory, and that's why the program compiles just fine in the editor and builds just fine—there isn't a problem until it's built out, and the file structure is a little different.*If you didn't get anything when you ran it via the command line, look for any errors that could be happening during that
initialize()
method; that's where your problem likely is. You can expose any exceptions during runtime by using a Popup Exception Dialog like shown in a similar problem, here.