JavaFX jar is not loading with JAR file but loads fine when compiling code as application
I can build and start the program just fine when compiling as an application in IntelliJ. However, nothing happens when I package and deploy and I try to double-click the jar. I decided to use Git Bash and run it from there and I see it is throwing a NullPointerException. Below is the JavaFX main class, my file structure, and the error that is being thrown.
Why am I getting a NullPointerException in the jar but works fine when I build and compile in IntelliJ?
Please let me know if I'm leaving any information out. Thanks!
public class Main extends Application {
private Stage stage;
@Override
public void start(Stage stage){
this.stage = stage;
this.stage.setTitle("My JavaFX App");
initRootlayout();
}
private void initRootlayout(){
try{
Parent root = FXMLLoader.load(getClass().getResource("/view/main.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
scene.getStylesheets().add("css/default.css");
stage.setResizable(false);
stage.show();
}
catch(Exception e){
throw new RuntimeException(e);
}
}
public static void main(String args) {
launch(args);
}
}


java javafx jar nullpointerexception javafx-8
add a comment |
I can build and start the program just fine when compiling as an application in IntelliJ. However, nothing happens when I package and deploy and I try to double-click the jar. I decided to use Git Bash and run it from there and I see it is throwing a NullPointerException. Below is the JavaFX main class, my file structure, and the error that is being thrown.
Why am I getting a NullPointerException in the jar but works fine when I build and compile in IntelliJ?
Please let me know if I'm leaving any information out. Thanks!
public class Main extends Application {
private Stage stage;
@Override
public void start(Stage stage){
this.stage = stage;
this.stage.setTitle("My JavaFX App");
initRootlayout();
}
private void initRootlayout(){
try{
Parent root = FXMLLoader.load(getClass().getResource("/view/main.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
scene.getStylesheets().add("css/default.css");
stage.setResizable(false);
stage.show();
}
catch(Exception e){
throw new RuntimeException(e);
}
}
public static void main(String args) {
launch(args);
}
}


java javafx jar nullpointerexception javafx-8
2
Your first image is fine, but the second one should not be an image at all. Post an exception stack trace as text, not as an image of text. Copy the text from your terminal or command window, and paste it into your question.
– VGR
Jan 19 at 4:54
UseParent root = FXMLLoader.load(getClass().getResource("view/main.fxml"));instead ofParent root = FXMLLoader.load(getClass().getResource("/view/main.fxml"));. That error (Location is required) has to do with the path.
– Cthulhu
Jan 19 at 4:55
@Cthulhu Since the Main class is in the unnamed package, there is no difference.
– VGR
Jan 19 at 4:56
add a comment |
I can build and start the program just fine when compiling as an application in IntelliJ. However, nothing happens when I package and deploy and I try to double-click the jar. I decided to use Git Bash and run it from there and I see it is throwing a NullPointerException. Below is the JavaFX main class, my file structure, and the error that is being thrown.
Why am I getting a NullPointerException in the jar but works fine when I build and compile in IntelliJ?
Please let me know if I'm leaving any information out. Thanks!
public class Main extends Application {
private Stage stage;
@Override
public void start(Stage stage){
this.stage = stage;
this.stage.setTitle("My JavaFX App");
initRootlayout();
}
private void initRootlayout(){
try{
Parent root = FXMLLoader.load(getClass().getResource("/view/main.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
scene.getStylesheets().add("css/default.css");
stage.setResizable(false);
stage.show();
}
catch(Exception e){
throw new RuntimeException(e);
}
}
public static void main(String args) {
launch(args);
}
}


java javafx jar nullpointerexception javafx-8
I can build and start the program just fine when compiling as an application in IntelliJ. However, nothing happens when I package and deploy and I try to double-click the jar. I decided to use Git Bash and run it from there and I see it is throwing a NullPointerException. Below is the JavaFX main class, my file structure, and the error that is being thrown.
Why am I getting a NullPointerException in the jar but works fine when I build and compile in IntelliJ?
Please let me know if I'm leaving any information out. Thanks!
public class Main extends Application {
private Stage stage;
@Override
public void start(Stage stage){
this.stage = stage;
this.stage.setTitle("My JavaFX App");
initRootlayout();
}
private void initRootlayout(){
try{
Parent root = FXMLLoader.load(getClass().getResource("/view/main.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
scene.getStylesheets().add("css/default.css");
stage.setResizable(false);
stage.show();
}
catch(Exception e){
throw new RuntimeException(e);
}
}
public static void main(String args) {
launch(args);
}
}


java javafx jar nullpointerexception javafx-8
java javafx jar nullpointerexception javafx-8
asked Jan 19 at 2:32
juicejuice
1,37062742
1,37062742
2
Your first image is fine, but the second one should not be an image at all. Post an exception stack trace as text, not as an image of text. Copy the text from your terminal or command window, and paste it into your question.
– VGR
Jan 19 at 4:54
UseParent root = FXMLLoader.load(getClass().getResource("view/main.fxml"));instead ofParent root = FXMLLoader.load(getClass().getResource("/view/main.fxml"));. That error (Location is required) has to do with the path.
– Cthulhu
Jan 19 at 4:55
@Cthulhu Since the Main class is in the unnamed package, there is no difference.
– VGR
Jan 19 at 4:56
add a comment |
2
Your first image is fine, but the second one should not be an image at all. Post an exception stack trace as text, not as an image of text. Copy the text from your terminal or command window, and paste it into your question.
– VGR
Jan 19 at 4:54
UseParent root = FXMLLoader.load(getClass().getResource("view/main.fxml"));instead ofParent root = FXMLLoader.load(getClass().getResource("/view/main.fxml"));. That error (Location is required) has to do with the path.
– Cthulhu
Jan 19 at 4:55
@Cthulhu Since the Main class is in the unnamed package, there is no difference.
– VGR
Jan 19 at 4:56
2
2
Your first image is fine, but the second one should not be an image at all. Post an exception stack trace as text, not as an image of text. Copy the text from your terminal or command window, and paste it into your question.
– VGR
Jan 19 at 4:54
Your first image is fine, but the second one should not be an image at all. Post an exception stack trace as text, not as an image of text. Copy the text from your terminal or command window, and paste it into your question.
– VGR
Jan 19 at 4:54
Use
Parent root = FXMLLoader.load(getClass().getResource("view/main.fxml")); instead of Parent root = FXMLLoader.load(getClass().getResource("/view/main.fxml"));. That error (Location is required) has to do with the path.– Cthulhu
Jan 19 at 4:55
Use
Parent root = FXMLLoader.load(getClass().getResource("view/main.fxml")); instead of Parent root = FXMLLoader.load(getClass().getResource("/view/main.fxml"));. That error (Location is required) has to do with the path.– Cthulhu
Jan 19 at 4:55
@Cthulhu Since the Main class is in the unnamed package, there is no difference.
– VGR
Jan 19 at 4:56
@Cthulhu Since the Main class is in the unnamed package, there is no difference.
– VGR
Jan 19 at 4:56
add a comment |
1 Answer
1
active
oldest
votes
check your jar content mostly it is missing your resources folder.
if you make the jar using IntelliJ Artifacts make sure to include your resources folder in jar, File -> Project Structure -> Artifacts -> Select your artifact -> output layout tab ->
-> Directory Content
-> Select your resources folder.
then rebuild your jar
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54263614%2fjavafx-jar-is-not-loading-with-jar-file-but-loads-fine-when-compiling-code-as-ap%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
check your jar content mostly it is missing your resources folder.
if you make the jar using IntelliJ Artifacts make sure to include your resources folder in jar, File -> Project Structure -> Artifacts -> Select your artifact -> output layout tab ->
-> Directory Content
-> Select your resources folder.
then rebuild your jar
add a comment |
check your jar content mostly it is missing your resources folder.
if you make the jar using IntelliJ Artifacts make sure to include your resources folder in jar, File -> Project Structure -> Artifacts -> Select your artifact -> output layout tab ->
-> Directory Content
-> Select your resources folder.
then rebuild your jar
add a comment |
check your jar content mostly it is missing your resources folder.
if you make the jar using IntelliJ Artifacts make sure to include your resources folder in jar, File -> Project Structure -> Artifacts -> Select your artifact -> output layout tab ->
-> Directory Content
-> Select your resources folder.
then rebuild your jar
check your jar content mostly it is missing your resources folder.
if you make the jar using IntelliJ Artifacts make sure to include your resources folder in jar, File -> Project Structure -> Artifacts -> Select your artifact -> output layout tab ->
-> Directory Content
-> Select your resources folder.
then rebuild your jar
answered Jan 19 at 6:25
Mahmoud FathyMahmoud Fathy
798
798
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54263614%2fjavafx-jar-is-not-loading-with-jar-file-but-loads-fine-when-compiling-code-as-ap%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
Your first image is fine, but the second one should not be an image at all. Post an exception stack trace as text, not as an image of text. Copy the text from your terminal or command window, and paste it into your question.
– VGR
Jan 19 at 4:54
Use
Parent root = FXMLLoader.load(getClass().getResource("view/main.fxml"));instead ofParent root = FXMLLoader.load(getClass().getResource("/view/main.fxml"));. That error (Location is required) has to do with the path.– Cthulhu
Jan 19 at 4:55
@Cthulhu Since the Main class is in the unnamed package, there is no difference.
– VGR
Jan 19 at 4:56