【发布时间】:2017-09-17 10:28:37
【问题描述】:
在过去的几天里,我尝试了很多方法,但都没有成功……我浏览了各种 Stackoverflow 条目,但无济于事……我一定遗漏了一些东西。
我尝试过三种不同的 IDE...IntelliJ、Eclispe 和 Netbeans。
问题是当我试图将我的程序变成可执行 jar 时,它无法运行(通过双击或通过命令运行)。
在执行以下命令时:
java -jar D:\Computing\Programming\Java\Projects\JavaFXGameMenu\out\artifacts\JavaFXGameMenu_jar\JavaFXGameMenu.jar
我得到:错误:无法找到或加载主类 root.Main
当我运行相同但使用 javaw 时.. 我没有收到错误消息,但也没有任何反应。
我主要使用 IntelliJ 作为我正在构建的 JavaFX 应用程序。
在创建 Artifact 时,我会根据其他线程选择以下内容:
然后我重新运行它:Java -jar D:\Computing\Executables\JavaFXGameMenu.jar
我遇到以下问题:
我已将相关环境变量放入我的系统和路径中,我正在使用 jre1.8.0_144。
非常感谢任何帮助追踪问题可能是什么
下面的代码...但这完全编译并在 IDE 中运行而没有错误...问题是当它变成 .jar 并从命令运行时。
package root;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Label;
import javafx.scene.effect.DropShadow;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.paint.CycleMethod;
import javafx.scene.paint.LinearGradient;
import javafx.scene.paint.Stop;
import javafx.scene.shape.Line;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Screen;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import javafx.stage.Window;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
public class Main extends Application {
private double width = 1920;
private double height = 1080;
private Parent createContent(){
Pane root = new Pane();
root.setPrefSize(width, height); //W:860 H:600
ImageView imgLogo = null;
ImageView bottomlogo = null;
MenuItem newGame = new MenuItem("NEW GAME");
MenuItem continueGame = new MenuItem("CONTINUE");
MenuItem friends = new MenuItem("FRIENDS");
MenuItem settings = new MenuItem("SETTINGS");
MenuItem store = new MenuItem("STORE");
MenuItem exit = new MenuItem("EXIT");
try(InputStream is = Files.newInputStream(Paths.get("src/resources/Images/dark.jpg"))) {
ImageView img = new ImageView(new Image(is));
img.setFitWidth(width);
img.setFitHeight(height);
root.getChildren().add(img);
} catch (IOException e){
System.out.println("Couldn't Load Image");
}
try(InputStream is = Files.newInputStream(Paths.get("src/resources/Images/logo.png"))) {
imgLogo = new ImageView(new Image(is));
imgLogo.setX(1000);
imgLogo.setY(100);
imgLogo.setFitWidth(600);
imgLogo.setFitHeight(300);
} catch (IOException e){
System.out.println("Couldn't Load Image");
}
try(InputStream is = Files.newInputStream(Paths.get("src/resources/Images/SteamAgony.png"))) {
bottomlogo = new ImageView(new Image(is));
bottomlogo.setX(100);
bottomlogo.setY(800);
bottomlogo.setFitHeight(200);
bottomlogo.setFitWidth(200);
bottomlogo.setOpacity(0.7);
} catch (IOException e){
System.out.println("Couldn't Load Image");
}
MenuBox menu = new MenuBox(
newGame,
continueGame,
friends,
settings,
store,
exit);
menu.setTranslateX(width / 3.4);
menu.setTranslateY(height / 2.5);
settings.setOnMouseClicked(event -> new SceneCreator().createScene(200,300));
exit.setOnMouseClicked( event -> Platform.exit());
root.getChildren().addAll(menu, imgLogo, bottomlogo);
return root;
}
@Override
public void start(Stage primaryStage) throws Exception{
Scene scene = new Scene(createContent());
primaryStage.setScene(scene);
primaryStage.initStyle(StageStyle.UNDECORATED);
primaryStage.show();
}
@Override
public void stop(){
//TODO
}
public static void main(String[] args) {
launch(args);
}
}
【问题讨论】:
-
能否包含添加空元素的代码
-
@Ravi 与该答案相差无几。这是我尝试过的线程之一并导致了同样的问题。
-
@jrtapsell 我已经包含了上面的代码,但是第 102 行引用了这个段,并且在 IDE 调试中检查显示它可以正常解析。 root.getChildren().addAll(menu, imgLogo, bottomlogo);
-
从 JAR 运行时路径无效。看到这个问题:stackoverflow.com/questions/20389255/… 或者可能这个问题:stackoverflow.com/questions/403256/…
标签: java javafx executable-jar