简体   繁体   中英

Start Java DB server without NetBeans

I am creating a Java APP that manages a database. I've been starting the JAVA DB server manually by right clicking - start server. with NetBeans but since I am not going to be the one that runs the application that can't be done anymore. I need a way to start it without NetBeans. Embedded or server mode, I don't really mind.

I've searched a lot but nothing seems to work. I tried this: Java DB - Starting Server within application

    NetworkServerControl server = new     NetworkServerControl(InetAddress.getLocalHost(),1527);
    server.start(null)    

I got java.net.ConnectException: Error al conectarse al servidor localhost en el puerto 1527 con el mensaje Connection refused: connect.

I tried also starting it with the command line

    String[] command =
{
    "cmd",
};
Process p = Runtime.getRuntime().exec(command);
new Thread(new SyncPipe(p.getErrorStream(), System.err)).start();
new Thread(new SyncPipe(p.getInputStream(), System.out)).start();
    try (PrintWriter stdin = new PrintWriter(p.getOutputStream())) {
        stdin.println("cd C:\\Program Files\\Java\\jdk1.8.0_31\\db\\lib");
        stdin.println("java -jar derbyrun.jar server start");
    }
int returnCode = p.waitFor();

Also got connection refused, (database Citas not found) so the only way it could work is this:

    String host = "jdbc:derby://localhost:1527/Citas";
    con = DriverManager.getConnection(host, username, password);    

But it works only if I start the server by clicking in Java DB -> start. Any help would be higly appreciated

Can you try starting it using Runtime ?

Runtime runtime = Runtime.getRuntime();
runtime.exec(new String[]{ "java", "-jar", "/path/to/derbyrun.jar", "server", "start");

If you were to do this in its own thread, you could also append .waitFor() to the .exec command which will hang until the process finishes. This would be good to determine if the db closed unexpectedly.

Also using runtime, you can get read the stdout / stderr of the process, and kill it if need be. Possibilities are endless.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM