我的可执行jar不起作用

时间:2015-04-21 03:17:29

标签: java eclipse javafx export executable-jar

我在“eclipse”中使用javafx的项目中一整夜都在工作。它工作得很好所以我按下“导出以获取可运行的jar文件,当我得到它时,它只是没有工作,但仍然没有。我猜问题是在主要方法,我将在这里发布。任何帮助会很棒。

package recetas;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.prefs.Preferences;

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.image.Image;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.stage.Modality;
import javafx.stage.Stage;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

import recetas.model.Receta;
import recetas.model.RecetasListWrapper;
import recetas.view.Controlador;
import recetas.view.ControladorEditar;
import recetas.view.VerControlador;



public class MainRecetas extends Application {
    private Stage primaryStage;
    private ObservableList<Receta> recetaData = FXCollections
            .observableArrayList();

    public MainRecetas() {
        recetaData.add(new Receta("Arroz tres delicias"));
        recetaData.add(new Receta("Pollo al horno"));
        recetaData.add(new Receta("Bizcocho de chocolate"));
        recetaData.add(new Receta("Pizza barbacoa"));
        recetaData.add(new Receta("Sopa de pescado"));
        recetaData.add(new Receta("Macarrones a la bolognesa"));
        recetaData.add(new Receta("Pechuga con patatas fritas"));
        recetaData.add(new Receta("Macedonia"));
        recetaData.add(new Receta("Fresas con chocolate"));
        recetaData.get(1).setVal(1);
        recetaData.get(1).setPlato("Plato");
        recetaData.get(1).setDif("Facil");
        recetaData.get(2).setDif("Dificil");
    }

    public ObservableList<HBox> getCajasList() {
        ObservableList<HBox> res = FXCollections.observableArrayList();
        for (int i = 0; i < recetaData.size(); i++)
            res.add(recetaData.get(i).getCaja());
        return res;
    }

    @Override
    public void start(Stage primaryStage) {
        this.primaryStage = primaryStage;
        this.primaryStage.setTitle("Mis recetillas");
        this.primaryStage.getIcons().add(new Image("@../../icono.png"));
        showPersonOverview();
    }

    public void showPersonOverview() {
        Controlador controller = null;
        try {
            FXMLLoader loader = new FXMLLoader();
            loader.setLocation(MainRecetas.class.getResource("view/Vista.fxml"));
            AnchorPane personOverview = (AnchorPane) loader.load();
            Scene scene = new Scene(personOverview);
            primaryStage.setScene(scene);
            controller = loader.getController();
            controller.setMainRecetas(this);
            primaryStage.show();
        } catch (IOException e) {
            e.printStackTrace();
        }
        File file = getFilePath();
        if (file != null){
            CargarRecetas(file);
            controller.setMainRecetas(this);
        }
    }

    public boolean editar(Receta r) {
        try {
            FXMLLoader loader = new FXMLLoader();
            loader.setLocation(MainRecetas.class
                    .getResource("view/VentanaEditar.fxml"));
            AnchorPane editar = (AnchorPane) loader.load();
            Stage dialog = new Stage();
            dialog.setTitle("Editar receta: " + r.getNombre());
            dialog.initModality(Modality.WINDOW_MODAL);
            dialog.initOwner(primaryStage);
            Scene scene = new Scene(editar);
            dialog.setScene(scene);
            ControladorEditar controller = loader.getController();
            controller.setDialogStage(dialog);
            controller.setReceta(r);
            dialog.showAndWait();
            return controller.getOK();
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }

    public boolean nuevo() {
        try {
            FXMLLoader loader = new FXMLLoader();
            loader.setLocation(MainRecetas.class
                    .getResource("view/VentanaEditar.fxml"));
            AnchorPane editar = (AnchorPane) loader.load();
            Stage dialog = new Stage();
            Receta r = new Receta(null);
            dialog.setTitle("Nueva receta");
            dialog.initModality(Modality.WINDOW_MODAL);
            dialog.initOwner(primaryStage);
            Scene scene = new Scene(editar);
            dialog.setScene(scene);
            ControladorEditar controller = loader.getController();
            controller.setDialogStage(dialog);
            controller.setReceta(r);
            dialog.showAndWait();
            boolean ok = controller.getOK();
            if (ok)
                recetaData.add(0, r);
            return ok;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }

    public void ver(Receta r) {
        try {
            FXMLLoader loader = new FXMLLoader();
            loader.setLocation(MainRecetas.class
                    .getResource("view/VerReceta.fxml"));
            AnchorPane editar = (AnchorPane) loader.load();
            Stage dialog = new Stage();
            dialog.setTitle(r.getNombre());
            dialog.initModality(Modality.WINDOW_MODAL);
            dialog.initOwner(primaryStage);
            Scene scene = new Scene(editar);
            dialog.setScene(scene);
            VerControlador controller = loader.getController();
            controller.setReceta(r);
            dialog.showAndWait();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void ayuda() {
        try {
            FXMLLoader loader = new FXMLLoader();
            loader.setLocation(MainRecetas.class
                    .getResource("view/Ayuda.fxml"));
            AnchorPane editar = (AnchorPane) loader.load();
            Stage dialog = new Stage();
            dialog.setTitle("Ayuda");
            dialog.initModality(Modality.WINDOW_MODAL);
            dialog.initOwner(primaryStage);
            Scene scene = new Scene(editar);
            dialog.setScene(scene);
            dialog.showAndWait();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public Stage getPrimaryStage() {
        return primaryStage;
    }

    public ObservableList<Receta> getRecetaData() {
        return recetaData;
    }

    public void eliminar(int i) {
        recetaData.remove(i);
    }

    public void setFilePath(File file) {
        Preferences prefs = Preferences.userNodeForPackage(MainRecetas.class);
        if (file != null) {
            prefs.put("filePath", file.getPath());

            primaryStage.setTitle("Mis recetillas - " + file.getName());
        } else {
            prefs.remove("filePath");

            primaryStage.setTitle("Mis recetillas");
        }
    }

    public File getFilePath() {
        Preferences prefs = Preferences.userNodeForPackage(MainRecetas.class);
        String filePath = prefs.get("filePath", null);
        if (filePath != null) {
            return new File(filePath);
        } else {
            return null;
        }
    }

    public void CargarRecetas(File file) {
        try {
            JAXBContext context = JAXBContext
                    .newInstance(RecetasListWrapper.class);
            Unmarshaller um = context.createUnmarshaller();

            RecetasListWrapper wrapper = (RecetasListWrapper) um
                    .unmarshal(file);

            recetaData.clear();
            try{ recetaData.addAll(wrapper.getRecetas());}
            catch(NullPointerException e){}

            setFilePath(file);

        } catch (JAXBException e) {
            Alert alert = new Alert(AlertType.ERROR);
            alert.setTitle("Error");
            alert.setHeaderText("No se pudo cargar la información del archivo:\n"
                    + file.getPath());
            alert.showAndWait();
        }
    }

    public void guardarRecetas(File file) {
        try {
            JAXBContext context = JAXBContext
                    .newInstance(RecetasListWrapper.class);
            Marshaller m = context.createMarshaller();
            m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

            RecetasListWrapper wrapper = new RecetasListWrapper();
            wrapper.setRecetas(recetaData);

            m.marshal(wrapper, file);

            setFilePath(file);
        } catch (Exception e) { 
            Alert alert = new Alert(AlertType.ERROR);
            alert.setTitle("Error");
            alert.setHeaderText("No se pudo guardar la información en el archivo:\n"
                    + file.getPath());
            alert.showAndWait();
        }
    }

    public List<Receta> buscarDestacadas() {
        List<Receta> l = new ArrayList<Receta>();
        for (int i = 0; i < recetaData.size(); i++) {
            Receta r = recetaData.get(i);
            if (r.getVal() == 5)
                l.add(r);
        }
        return l;
    }

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

2 个答案:

答案 0 :(得分:0)

由于评论中描述的事件以及您提供的代码,我有两种可能的解决方案。

  1. 根据您提供的代码,没有launch()方法。这意味着一旦启动,程序将无法执行,并且应该产生错误,因为没有方法launch()
  2. 您可能已将其导出为可执行文件以外的其他内容。也许您已将其导出为jar file而非runnable jar file。如果您这样做,您只需要更改导出方式。

答案 1 :(得分:0)

我也有同样的问题,但我在start方法中初始化了照片(仅在JavaFX中使用),我的问题已经解决,jar的工作方式与在IDE中工作相同(在我的情况下是Eclipse)。

@Override
public void start(Stage stage) {
  // set all photos in this function
}

请注意Eclipse IDE:

  1. 在已安装的JRE中设置JDK而不是JRE
  2. 从File-&gt; New-&gt; Project-&gt; JavaFX-&gt; JavaFX项目创建特殊的JavaFX项目
  3. 通过&#34; build.fxbuild&#34;构建项目然后,像普通的Java程序一样导出
  4. 在开始方法中设置所有图标和照片,从不在其他功能中定义图标或任何照片。