简体   繁体   中英

How to start msaccess database from command line in java?

I'm working on a database-related project and i have a MS-Access Database located at:

D:\\My Documents\\Database.accdb

So i use the following command for running MS-Access:

cmd /c start MSACCESS D:\\My Documents\\database.accdb

In java:

view.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent E) {
        String[] command = {"cmd", "/c", "start MSACCESS D:\\My Documents\\Database.accdb"}
        Runtime.getRuntime().exec(command);
    }
});

Microsoft Access inmediatly starts but throws the following error:

"The command line you used to start Microsoft Office Access contains an option that Micosoft Access doesn't recognize"

And then:

"The database file 'D:\\My.mdb' cannot be found"

So i assumed that it doesn't recognise the space at: "My Documents" and it truncates at "My", so i try it's NTFS 8.3 equivalent: D:\\MYDOCU~1\\Database.accdb, and throws:

"The database file 'D:\\MYDOCU~1\\Database.accdb' cannot be found"

I really don't know whats wrong

"D:\\My Documents\\Database.accdb" contains a space and is being treated as an additional parameter (so it would like like {"cmd", "/c", "start", "MSACCESS", "D:\\\\My", "Documents\\\\Database.accdb"} to the calling program)

Use something like

String[] command = {"cmd", "/c", "start", "MSACCESS", "D:\\My Documents\\Database.accdb"};

instead...

UPDATE

Trying to "shorten" the path names manually is very dangerous and ill advised. If you need to go this route, you really should take advantage of a native call to the OS to tell how the name should be shortened, so both you and the OS know that you're talking about the same thing

ADDITIONAL

Also, try running the command from the command prompt just to make sure it works ;)

Try this instead:

String[] command = {"cmd", "/c", "start \"MSACCESS D:\\My Documents\\Database.accdb\""}

PS: I don't know about your project, but depending on what you want to do with the database, it could make sense to open it as a "normal" SQL database via JDBC instead. Post another question if you'd like to do that and have trouble with the connection string.

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