JavaFX app doesn't work as executable jar due to ClassNotFoundException, but Maven imports all dependencies without any issues.

Answered

Hello support team and community! 

I'm trying to make a simple app - URL parser, using JSoup.

I tried/searched many ways to solve this problem(ClassNotFoundException), but I can't get to work my test simple app.

Also tried this https://www.jetbrains.com/help/idea/2016.1/configuring-module-dependencies-and-libraries.html?origin=old_help&search=dependencies#new_lib_add_to_module

No changes, the same exception. I added "jsoup-1.11.3.jar" to lib folder as library, but still the same error. 

Forgot to add, it works perfectly in IDE. I want to develop with pleasure, but I can't. Thanks in advance! 

Kindly see attached screenshots

Main class:

Controller class:

pom.xml:

sample.fxml:

 

Project:

 

java.lang.ClassNotFoundException: org.jsoup.Jsoup

 

Leaving here my "Hello world" code, if you guys need it to replicate mentioned exception.

Main class:

package sample;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Main extends Application {

@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getClassLoader().getResource("sample/sample.fxml"));
primaryStage.setTitle("Hello World");
primaryStage.setScene(new Scene(root, 300, 275));
primaryStage.show();
}


public static void main(String[] args) {
launch(args);
}
}

Controller class:

package sample;

import javafx.event.ActionEvent;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import java.io.IOException;

public class Controller {
public void open(ActionEvent event) {

Document doc = null;
try {
doc = Jsoup.connect("http://jsoup.org").get();
} catch (IOException e) {
e.printStackTrace();
}

Element link = doc.select("a").first();
String relHref = link.attr("href"); // == "/"
String absHref = link.attr("abs:href"); // "http://jsoup.org/"
System.out.println(relHref);
System.out.println(absHref);
}
}

pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>groupId</groupId>
<artifactId>JSoupFX</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<resources>
<resource>
<directory>C:\Users\User\IdeaProjects\JSoupFX\src\main\java\sample</directory>
<targetPath>sample</targetPath>
</resource>
</resources>
</build>
<dependencies>

<dependency>
<!-- jsoup HTML parser library @ https://jsoup.org/ -->
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.11.3</version>
</dependency>

</dependencies>

</project>

sample.fxml:

<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.layout.ColumnConstraints?>
<GridPane alignment="center" hgap="10" prefHeight="275.0" prefWidth="300.0" vgap="10" xmlns="http://javafx.com/javafx/8.0.172-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.Controller">
<columnConstraints>
<ColumnConstraints />
</columnConstraints>
<rowConstraints>
<RowConstraints />
</rowConstraints>
<children>
<Button mnemonicParsing="false" onAction="#open" text="Button" />
</children>
</GridPane>

 

0
1 comment

Hello,

There is a known issue with JAR containing other JARs: https://stackoverflow.com/questions/44283408/is-it-possible-to-get-intellij-idea-to-generate-an-executable-jar-manifest-like. IDE by default doesn't support such configuration, so you could use maven goal to build artifact.

0

Please sign in to leave a comment.