How to run database program outside the Netbeans IDE?

五迷三道 提交于 2019-12-01 21:28:08
Nick Holt

Right, from your description there seems to be a couple of things you are confusing.

First, databases are typically run as servers with multiple clients connecting to them thus allowing they contain to be shared. When you start Java DB you are starting the Java DB database server.

That said, lightweight databases, such as Java DB can be run in an embedded mode as explained here. Remember that the directory you point to with the derby.system.home property will need to contain the database files, if not you'll need to create that programatically too.

Second, there's various ways to run a Java application outside of an IDE, but jars themselves are not executable in the same way an exe file is in Windows.

The simplest way is to call the java executable passing the necessary classpath and the name of the class containing the main method. For example if I had a class called com.example.Application that had been compiled to a directory C:\dev\example\classes the following command line would run the application:

java -cp C:\dev\example\classes com.example.Application

If there were dependencies on external libraries, as there will be in your case on the Derby JDBC driver, then those would also need including in the classpath resulting in something like:

java -cp C:\dev\example\classes;C:\dev\lib\derby.jar com.example.Application

There's a full set of document on the Java launcher here.

Now, back to the jar. Like I said, jars are not executable but there is something that's know as an 'executable jar'. This is the same as any jar except there are some special additions to the manifest to specify the application entry point or main-class and the class-path as described here.

Once the main-class and class-path are specified in the jar's manifest, the following command line would run the application:

java -jar C:\dev\example.jar

You can even associate the jar extension with the java exe and double clicking the jar will cause the application to launch (though on a dev machine it's probably more useful that the jar extension be associated with WinZip or simlar in order to open that jar).

The default database in Netbeans is Derby/JavaDB. So you need to:

  • add the jar of javadb/derby in our classpath (it maybe already present, as it is bundled with java when you install it in Ubuntu)
  • setup a path with the jdbc URI to save the database data

I personally recommend the usage of hsqldb or H2 for this: they support in-memory database, very useful for stand alone project with no persistence data or for tests.

If you use window, add ODBC Data Sources from Administrative Tools to your Java Derby driver and run jar.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!