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.