简体   繁体   English

如何在Java中从命令行启动msaccess数据库?

[英]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: 我正在从事与数据库相关的项目,并且我的MS-Access数据库位于:

D:\\My Documents\\Database.accdb

So i use the following command for running MS-Access: 因此,我使用以下命令来运行MS-Access:

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

In java: 在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: Microsoft Access会立即启动,但会引发以下错误:

"The command line you used to start Microsoft Office Access contains an option that Micosoft Access doesn't recognize" “您用于启动Microsoft Office Access的命令行包含Micosoft Access无法识别的选项”

And then: 接着:

"The database file 'D:\\My.mdb' cannot be found" “找不到数据库文件'D:\\ My.mdb'”

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: 因此,我假定它无法识别“我的文档”中的空格,并且将其截断为“我的”,因此我尝试使用等效于NTFS 8.3的文件:D:\\ MYDOCU〜1 \\ Database.accdb,并抛出:

"The database file 'D:\\MYDOCU~1\\Database.accdb' cannot be found" “找不到数据库文件'D:\\ MYDOCU〜1 \\ Database.accdb'”

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) “ D:\\ My Documents \\ Database.accdb”包含一个空格,并被视为附加参数(因此,它像{"cmd", "/c", "start", "MSACCESS", "D:\\\\My", "Documents\\\\Database.accdb"}到调用程序)

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 如果您需要走这条路,那么您确实应该利用对OS的本地调用来告诉您应该如何缩短名称,因此您和OS都知道您在谈论同一件事

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. PS:我不了解您的项目,但是根据您想对数据库进行的操作,可以通过JDBC将其作为“普通” SQL数据库打开是有意义的。 Post another question if you'd like to do that and have trouble with the connection string. 如果您想这样做并发表疑问,请提出另一个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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