【发布时间】:2016-10-24 14:37:52
【问题描述】:
我使用 JavaFx 和 Hibernate 在 Java 中创建了一个应用程序。 在我的 IDE 中一切正常。但是当我构建应用程序并启动 .jar 文件时,所有使用 Hibernate 的功能都不起作用。
我创建了另一个 testapp 来向您展示我的代码:
这是我的主要课程:
package testapp;
import java.util.List;
import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.ComboBox;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
public class Testapp extends Application {
@Override
public void start(Stage primaryStage) {
ComboBox<Test> cmbtest=new ComboBox<>();
ObservableList<Test> l=FXCollections.observableArrayList(loadCmb());
StackPane root = new StackPane();
root.getChildren().add(cmbtest);
cmbtest.setItems(l);
Scene scene = new Scene(root, 300, 250);
primaryStage.setTitle("Hello World!");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
public List<Test> loadCmb(){
List<Test> list;
SessionFactory sf;
sf= NewHibernateUtil.getSessionFactory();
Session session = sf.openSession();
Query query=session.createQuery("FROM Test test");
list=query.list();
return list;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test?zeroDateTimeBehavior=convertToNull</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">****</property>
<mapping resource="testapp/Test.hbm.xml"/>
</session-factory>
</hibernate-configuration>
这是我的 NewHibernateUtil:
package testapp;
import java.io.File;
import javax.imageio.spi.ServiceRegistry;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
public class NewHibernateUtil {
private static SessionFactory sf;
private static ServiceRegistry serviceRegistrry;
static {
try {
File file=new File("src/testapp/hibernate.cfg.xml");
Configuration cfg= new Configuration().configure(file);
StandardServiceRegistryBuilder sb= new StandardServiceRegistryBuilder();
sb.applySettings(cfg.getProperties());
StandardServiceRegistry standardServiceRegistry= sb.build();
sf = cfg.buildSessionFactory(standardServiceRegistry);
} catch (Throwable ex) {
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sf;
}
}
这是我唯一的课程,由我在 MySql 中的数据库创建
package testapp;
// Generated 24/10/2016 01:53:10 PM by Hibernate Tools 4.3.1
/**
* Test generated by hbm2java
*/
public class Test implements java.io.Serializable {
private String name;
private Integer number;
public Test() {
}
public Test(String name) {
this.name = name;
}
public Test(String name, Integer number) {
this.name = name;
this.number = number;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public Integer getNumber() {
return this.number;
}
public void setNumber(Integer number) {
this.number = number;
}
}
mySql 是一张有 2 列的表 测试表: 姓名和号码
如果我在我的 IDE 中运行此程序,则应用程序运行正常,但是当我构建应用程序时,.jar 文件无法正常工作。
这是我构建的输出:
ant -f C:\\Users\\Dani-Fla-Mathi\\Documents\\NetBeansProjects\\testapp jfx-rebuild
init:
deps-clean:
Created dir: \testapp\build
Updating property file: \testapp\build\built-clean.properties
Deleting directory \testapp\build
clean:
init:
deps-jar:
Created dir: \testapp\build
Updating property file: \testapp\build\built-jar.properties
Created dir: \testapp\build\classes
Created dir: \testapp\build\empty
Created dir: \testapp\build\generated-sources\ap-source-output
Compiling 3 source files to \testapp\build\classes
Note:\testapp\src\testapp\Testapp.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
Copying 3 files to \testapp\build\classes
compile:
Created dir: \testapp\dist
Copying 17 files to \testapp\dist\lib
Detected JavaFX Ant API version 1.3
Launching <fx:jar> task from C:\Program Files\Java\jdk1.8.0_05\jre\..\lib\ant-javafx.jar
Warning: From JDK7u25 the Codebase manifest attribute should be used to restrict JAR repurposing.
Please set manifest.custom.codebase property to override the current default non-secure value '*'.
Launching <fx:deploy> task from C:\Program Files\Java\jdk1.8.0_05\jre\..\lib\ant-javafx.jar
jfx-deployment-script:
jfx-deployment:
jar:
jfx-rebuild:
BUILD SUCCESSFUL (total time: 2 seconds)
如果您需要更多信息,请告诉我
【问题讨论】:
-
抱歉这个 build* using*
-
没有任何代码,没有错误消息,有人怎么能推荐你缺少的东西?
-
任何人都需要相当多的信息才能回答这个问题(而不仅仅是猜测大量可能出错的事情)。请阅读stackoverflow.com/help/how-to-ask 并尝试根据那里的指南来构建您的问题。
-
我假设他没有创建正确的类路径,只是运行 javafx 应用程序的 jar。有多种选择。阅读以下内容很可能是个好主意docs.oracle.com/javase/8/docs/technotes/tools/windows/…
-
@DimaSan 这是问题所在。我在应用程序中没有收到任何错误。例如。我有一个打开场景的按钮。在这个场景中,我有一个组合框,其中填充了我的数据库中的一些信息。当我单击此按钮时,场景未打开
标签: java hibernate javafx build netbeans-8