gpt4 book ai didi

java - Runtime.exec 字符串给出语法错误

转载 作者:行者123 更新时间:2023-11-30 02:42:16 24 4
gpt4 key购买 nike

我正在使用runtime.exec尝试将.SQL脚本执行到我本地的SQLite数据库文件中。我目前正在 MacOS 上运行它。

    Runtime runtime = Runtime.getRuntime();
try
{
Process process = runtime.exec("sqlite3 /Users/Documents/Uni/Music\\ Player/src/Common/database.db < /Users/Documents/Uni/Music\\ Player/src/Common/database.sql");

BufferedReader stdError = new BufferedReader(newInputStreamReader(process.getErrorStream()));

try {
if (process.waitFor() != 0) {
String s = "";
System.err.println("exit value = " + process.exitValue());
System.out.println("Here is the standard error of the command (if any):");
while ((s = stdError.readLine()) != null) {
System.out.println(s);
}
}
} catch (InterruptedException e) {
System.err.println(e);
}

这是我当前正在使用的代码。如果我通过终端执行该命令,它将成功执行,并且我的数据库将使用 .SQL 脚本的内容进行更新。

但是,当我通过代码执行此操作时,出现错误:

错误:“玩家”附近:语法错误伴随着进程退出

我一生都无法找出字符串语法的问题。我尝试了多种不同的方法来转义字符串中的空格,但未成功。有任何想法吗?

最佳答案

而不是尝试使用手动硬编码生成文件路径 String ,让我们做一个 Path 并让它为您生成文件路径。这使用起来不太困难,并且无论操作系统或文件系统是什么,它都有生成正确文件路径的好处。

用此替换当前的 exec 命令以实现该更改:

Path parentDirectory = Paths.get("Users","Documents","Uni","Music Player","src","Common");
String sqlCommand = "sqlite3 database.db < database.sql";
Process process = runtime.exec(sqlCommand, null, parentDirectory.toFile());

这将使用从父进程(JVM)继承的环境变量运行“sqlite3 database.db parentDirectory 指定的目录中运行该命令。 ,这恰好是您在问题中给出的目录。

但是,尽管修复了原始问题,您的代码还包含另一个与文件重定向的使用相关的问题。文件重定向通过 <>是一个 shell 构造,在 shell 之外无法工作。使用 Runtime.exec() 基本上就像使用 Run... Windows 上的 GUI,由于文件重定向在那里不起作用,所以在这里也不起作用。您需要修复所使用的命令才能解决此问题。幸运的是,有一个 StackOverflow question addressing this exact problem here .

这是解决了两个问题的代码:

Path parentDirectory = Paths.get("Users","Documents","Uni","Music Player","src","Common");
String sqlCommand = "sqlite3 database.db";
File inputSqlFile = new File("database.sql");

Process process = new ProcessBuilder()
.directory(parentDirectory.toFile())
.command(sqlCommand)
.redirectInput(Redirect.from(inputSqlFile))
.start();

此代码使用 ProcessBuilder 将目录设置为与以前相同的父目录,但该命令已被修改以删除文件重定向,而是使用 redirectinput() 实现文件重定向。方法。这完成了与 "sqlite3 database.db < database.sql" 相同的事情如果在 shell 中执行,则该代码将在 Java 中运行。

<小时/>

编辑:

上面的代码不起作用,因此我尝试修复它,并添加了调试语句来帮助跟踪问题:

Path databasePath = FileSystems.getDefault().getPath("Users", "Documents", "Uni", "Music Player", "src", "Common", "database.db");
Path sqlPath = FileSystems.getDefault().getPath("Users", "Documents", "Uni", "Music Player", "src", "Common", "database.sql");

File database = databasePath.toFile().getAbsoluteFile();
File sqlFile = sqlPath.toFile().getAbsoluteFile();

System.out.println("Database location:\n\t" + database.getCanonicalPath());
System.out.println("SQL file location:\n\t" + sqlFile.getCanonicalPath());

assert database.exists() && database.isFile() && database.canRead() && database.canWrite(): "No such database file!";
assert sqlFile.exists() && sqlFile.isFile() && database.canRead() : "No such SQL file!";

String[] sqlCommand = {"sqlite3", database.getCanonicalPath()};
Redirect sqlInput = Redirect.from(sqlFile);
File workingDirectory = database.getParentFile().getCanonicalFile();

System.out.println("Running this command:\n\t" +
sqlCommand[0] + sqlCommand[1] + "<" + sqlInput.file().getCanonicalPath());

Process process = new ProcessBuilder()
.directory(workingDirectory)
.command(sqlCommand)
.redirectInput(sqlInput)
.start();

这是相同的代码,删除了调试代码并进行了轻微的清理:

File database = FileSystems.getDefault()
.getPath("Users", "Documents", "Uni","Music Player", "src", "Common", "database.db")
.toFile().getAbsoluteFile();
File sqlFile = FileSystems.getDefault()
.getPath("Users", "Documents", "Uni","Music Player", "src", "Common", "database.sql")
.toFile().getAbsoluteFile();

String[] sqlCommand = {"sqlite3", database.getCanonicalPath()};
Redirect sqlInput = Redirect.from(sqlFile);
File workingDirectory = database.getParentFile().getCanonicalFile();

Process process = new ProcessBuilder()
.directory(workingDirectory)
.command(sqlCommand)
.redirectInput(sqlInput)
.start();

关于java - Runtime.exec 字符串给出语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41291785/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com