Возникла проблема при сборке проекта в IntelliJ IDEA. Использовал Maven и JavaFX.
Суть программы считать данные с поля ввода текста, оправить на сервер запрос(поверка номера) и в случае, если номер есть в бд, идет переход на след. сцену.
При тестировании в IntelliJ IDEA все работает как часы, но при попытке запустить jar появляется ошибка:
Error invoking method
Разбираясь с проблемой пришел к тому, что если убрать код метода запроса на сервер (метод aurorization()
), jar запускается и производится переход к след. сцене, но если "включить" метод и даже если не использовать метод, возникает ошибка:
Error invoking method
Для удобства код обработки нажатия кнопки и код отправки запроса на сервер представлены в контроллере.
MainApp
package com.devcolibri.mavenjavafxapp;
public class MainApp extends Application {
public static void main(String[] args) throws Exception {
launch(args);
}
@Override
public void start(Stage stage) throws Exception {
String fxmlFile = "/fxml/login.fxml";
FXMLLoader loader = new FXMLLoader();
Parent root = loader.load(getClass().getResourceAsStream(fxmlFile));
stage.setTitle("YouDelivery");
stage.setResizable(false);
stage.setScene(new Scene(root));
stage.getIcons().add(new Image("/ic/YD-256.png"));
stage.show();
}
}
Контроллер
package com.devcolibri.mavenjavafxapp.controllers;
public class LoginControllerFX {
@FXML
private TextField edLogin;
public void logIn(ActionEvent actionEvent) {
Stage stageIn = (Stage)
((Node)actionEvent.getSource()).getScene().getWindow();
String login = edLogin.getText();
Resp authorization = aurorization(login, "1");
if(true){
try {
FXMLLoader loader = new
FXMLLoader(getClass().getResource("/fxml/start_order.fxml"));
Parent root = loader.load();
stageIn.setScene(new Scene(root, 850, 450));
stageIn.show();
} catch (Exception e) {
e.printStackTrace();
}
}
else
showAlertError("Ошибка авторизации!");
}
private void showAlertError(String text) {
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setTitle("Авторизация");
alert.setHeaderText(null);
alert.setContentText(text);
alert.showAndWait();
}
private Resp aurorization(String login, String pass) { // Метод авторизации
Gson GSON = new Gson();
String appKey = "c312b5a7b1794220a85b89079250e64e";
String cliKey = "aec9813472954766897c74a55815d4e1";
final String j =
"{\n" +
"\"app\":\"" + appKey + "\",\n" +
"\"cli\":\"" + cliKey + "\",\n" +
"\"email\":\"" + login + "\",\n" +
"\"password\":\"1\"\n" +
"}";
HttpClient httpClient = HttpClients.createDefault();
String url = "https://api.scorocode.ru/api/v1/login";
HttpPost httpPost = new HttpPost(url);
httpPost.addHeader(HTTP.CONTENT_TYPE, "application/json");
StringEntity stringEntity = new StringEntity(j, org.apache.http.entity.ContentType.APPLICATION_JSON);
httpPost.setEntity(stringEntity);
try {
HttpResponse response = httpClient.execute(httpPost);
int code = response.getStatusLine().getStatusCode();
if (code == 200) {
HttpEntity respEntity = response.getEntity();
String result = EntityUtils.toString(respEntity);
Resp resp = GSON.fromJson(result, Resp.class);
if (!resp.getError() &&
!resp.getResult().getUser().getIsBlocked()) {
return resp;
}
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
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>
<dependencies>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.5</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20180130</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.4</version>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver-sync</artifactId>
<version>3.8.0</version>
</dependency>
<dependency>
<groupId>com.akathist.maven.plugins.launch4j</groupId>
<artifactId>launch4j-maven-plugin</artifactId>
<version>1.5.2</version>
</dependency>
</dependencies>
<groupId>prob2</groupId>
<artifactId>prob2</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<build>
<plugins>
<plugin>
<groupId>com.zenjava</groupId>
<artifactId>javafx-maven-plugin</artifactId>
<version>2.0</version>
<configuration>
<mainClass>com.devcolibri.mavenjavafxapp.MainApp</mainClass>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
Ошибка из консоли
Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$154(LauncherImpl.java:182)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NoClassDefFoundError: org/apache/http/HttpEntity
at java.lang.Class.getDeclaredConstructors0(Native Method)
at java.lang.Class.privateGetDeclaredConstructors(Unknown Source)
at java.lang.Class.getConstructor0(Unknown Source)
at java.lang.Class.newInstance(Unknown Source)
at sun.reflect.misc.ReflectUtil.newInstance(Unknown Source)
at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:927)
at javafx.fxml.FXMLLoader$InstanceDeclarationElement.processAttribute(FXMLLoader.java:971)
at javafx.fxml.FXMLLoader$Element.processStartElement(FXMLLoader.java:220)
at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:744)
at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2707)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2527)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2425)
at com.devcolibri.mavenjavafxapp.MainApp.start(MainApp.java:22)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
... 1 more
Caused by: java.lang.ClassNotFoundException: org.apache.http.HttpEntity
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 22 more
Exception running application com.devcolibri.mavenjavafxapp.MainApp
Сборка проекта
Project Structure -> Artifactc
Далее Build-> Build Artifacts-> Build
java -jar <filename.jar>
и скопировать выхлоп.