Casey B.
Casey B.

Reputation: 279

JavaFX: Need help understanding project hierarchy issues

My code has gotten quite large and so I've decided to build a runnable JAR to see how it's done. I probably should've tried sooner because I'm getting 2 different errors related to the project structure, first is the "no main manifest attribute error" when trying to run the JAR from command prompt. Double-clicking the JAR does nothing (Win7). The second issue is related to the FXMLLoader explained lower down.

I followed the steps here to build the JAR, which involved moving all Maven files into the JAR directory. The compiled JAR gave me the manifest error, so I followed this which adds a Maven plugin in my pom.xml file. The error might be caused by a wrong naming convention with the line <mainClass>com.primary.Drag</mainClass> where primary is the package and Drag is my Drag.java file (class) which has the main method.

Inititally I was using the default package but read that this is not recommended for larger projects, so I put all my files into "primary". Here is my current hierarchy as shown in IntelliJ:

hierachy

The problem is that ever since I created the "primary" package, I can no longer even compile the program via IntelliJ, let alone build a runnable JAR. This is due by the second error I mentioned, which is java.lang.IllegalStateException: Location is not set. on this line within primary/Drag.java:

FXMLLoader loader = new FXMLLoader(getClass().getClassLoader().getResource("firstlaunch.fxml")); It used to work with the default package, but not anymore.

I tried replacing firstlaunch.fxml with /primary/firstlaunch.fxml and /resources/firstlaunch.fxml (with and without moving resources into primary package) but no luck.

3 Related Questions:

  1. Is my project structure incorrect?
  2. How do I reference the fxml file from the primary package?
  3. Is this what I should write in Maven's mainClass tags? <mainClass>com.primary.Drag</mainClass>

Upvotes: 1

Views: 123

Answers (1)

Shineed Basheer
Shineed Basheer

Reputation: 578

  1. Is my project structure incorrect?

Answer:

Your package name should be like com.primary.******

  1. How do I reference the fxml file from theprimary package?

Answer:

Always make sure that you are trying to load firstlaunch .xml from the class which is located in same package where that xml is kept. Means class which you wrote the loading code and xml file should be in same package

  1. Is this what I should write in Maven's mainClass tags?com.primary.Drag

Answer:

If you package name corrected to com.primary , your main class Drag will correctly added by maven

Upvotes: 1

Related Questions