0

I am using JDK 16 and I want to ask a question about building the JAR of a JavaFX application. I happen to have an application that depends on mssql-jdbc, my application runs inside the IDE (Intellij). When I send to generate the JAR I follow the following steps:

Project Structure -> Artifacts -> Add -> Jar -> From module with dependencies… -> Add -> SDK’s bin content -> OK -> Artifacts -> Build

My JAR is generated without any problem, but when I try to execute it, it does not open and if I try to use the java -jar command, I get the following error:

Error: Could not find or load main class sample.App
Caused by: java.lang.ClassNotFoundException: sample.App
Caused by: java.lang.ClassNotFoundException: sample.App

My PC configuration is as follows:

JAVA_HOME = C:\Program Files\Java\jdk-16.0.1
PATH = C:\Program Files\Java\jdk-16.0.1\bin
CLASSPATH = C:\Program Files\Java\jars\mssql-jdbc-9.4.1.jre16.jar;.;

java -version 
java version "16.0.1" 2021-04-20
Java(TM) SE Runtime Environment (build 16.0.1+9-24)
Java HotSpot(TM) 64-Bit Server VM (build 16.0.1+9-24, mixed mode, sharing)

I created a simple test project and when I add the mssql dependency the same thing happens. I need to know how to generate my JAR and see it working without using Maven, Gradle or any other tool.

The test project I created consists of 3 classes. Class #1 is the class that inherits from the Application class and contains the start() and main() methods. The class #2 has a main method that is the one that calls the main that is in the class #1 and is the one that I use for the construction of the artifact. Class #3 is the one that has the logic to connect to the database.

I really hoped that with the creation of this test project with a single dependency (mssql-jdbc) it would work and I could notice a configuration error.

I have not found the solution no matter how much I have searched the internet. However I did do something to see if I was working right, I compiled a class #4 and ran its .class and it worked.

The problem is the JAR, not the code. I don't know what the IDE needs to be configured.

jewelsea
  • 150,031
  • 14
  • 366
  • 406
Bart
  • 1
  • 1
    One potential solution is [this one on creating a fat jar using maven](https://stackoverflow.com/questions/52653836/maven-shade-javafx-runtime-components-are-missing). – jewelsea Aug 05 '23 at 20:48
  • what does `jdbc` have to do with it? platform does not find *sample.App* – mr mcwolf Aug 06 '23 at 06:23
  • @mrmcwolf The problem is with the JDBC dependency. JDBC is the only dependency of this test project. If I remove the JDBC the JAR is generated without problems and it runs with a double click. But if the JDBC is kept as a dependency the JAR is generated without problems, but it does not run. And I want to know how to make my JAR work without using Maven or Gradle. – Bart Aug 06 '23 at 14:56
  • @jewelsea I want to create the JAR without using Maven or Gradle. Is that possible? And if so, how do I do it? – Bart Aug 06 '23 at 15:00
  • the error you are getting is quite clearly described. it has nothing to do with `jdbc`. nothing else can be said based on a post without any other information – mr mcwolf Aug 06 '23 at 15:54
  • You can create a jars without Maven or Gradle from the command line using the [jar](https://docs.oracle.com/en/java/javase/17/docs/specs/man/jar.html) tool in the JDK. The [java](https://docs.oracle.com/en/java/javase/17/docs/specs/man/java.html) command documents how the jars can be used by the Java launcher. – jewelsea Aug 06 '23 at 16:54
  • @jewelsea To create the JAR of my JavaFX application I have to add the content of the bin folder of my SDK. How do I proceed to build my JAR? Why doesn't the IDE have its own option to take care of that? – Bart Aug 07 '23 at 12:26
  • “I have to add the content of the bin folder of my SDK” -> the JavaFX binary components are included in both the JavaFX jars in the Maven central repository and In JREs that include JavaFX, so relying on either of those rather than binary components from the JavaFX SDK are both options, or you can use jlinked images. Info on some of those options is in the “Runtime Images” documentation in the getting started instructions at openjfx.io. – jewelsea Aug 07 '23 at 15:24
  • “Why doesn't the IDE have its own option to take care of that?” -> I don’t know whether your IDE can copy binaries into jars from different directories or not, without relying on a 3rd party build tool such as Maven or Gradle. If you have a query about that, you can request support directly from the IDE vendor. – jewelsea Aug 07 '23 at 15:32
  • “How do I proceed to build my JAR?” -> I already linked to a methods to do that using Maven or command line tools. If it is not what you want, I don’t have another solution for you. – jewelsea Aug 07 '23 at 15:36

0 Answers0