2

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

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Joan Gil
  • 21
  • 4

1 Answers1

0

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.

Matt Clark
  • 27,671
  • 19
  • 68
  • 123
  • I got: Se ha rechazado la conexión porque no se ha encontrado la base de datos Citas. In english it would be something like: Connection rejected because database Citas was not found. It's pretty the same I did by using windows command line. I'll edit my question to show you. – Joan Gil Aug 11 '15 at 18:24
  • "database Citas not found" is straightforward to fix. Since your connection URL specified a simple database name, Derby is looking in the location where you started your Network Server. You can specify an absolute location for the DB in your connection URL, or specify derby.system.home when you start the Network Server. If the database truly doesn't exist, specify 'create=true'. – Bryan Pendleton Aug 15 '15 at 15:56