当我尝试运行以下 build.xml 文件以从 eclipse 中的 java 应用程序使用 mysql 时,收到以下错误消息:
构建失败
d:\mypath\workspace\appname\database\build.xml:14: 执行失败: java.io.IOException: 无法运行程序“mysql”: CreateProcess error=2,系统找不到指定的文件
这是 build.xml 的代码:
<?xml version="1.0" encoding="UTF-8"?>
<project name="publisher" default="all" basedir=".">
<property name="mysql.params" value="-u publisher -ppublisher -D publisher" />
<target name="all" depends="cleandb, createdb, insertdb"></target>
<target name="cleandb">
<exec executable="mysql" input="cleandb.sql">
<arg line="${mysql.params}" />
</exec>
</target>
<target name="createdb">
<exec executable="mysql" input="createdb.sql">
<arg line="${mysql.params}" />
</exec>
</target>
<target name="insertdb">
<exec executable="mysql" input="insertdb.sql">
<arg line="${mysql.params}" />
</exec>
</target>
</project>
在 Eclipse 中,我将文件作为“Ant Build...”运行,并首先调用 createdb.sql。因此,抛出的行是被要求执行的第一行,即:
问题似乎是脚本无法找到 mysql。从 Windows 命令行,我可以使用以下路径调用数据库实例:
d:\mypath\MySQL\MySQL Server\bin\mysqld
我认为xml文件需要指定mysql安装的路径,但我不知道如何做到这一点,同时还传递上面代码中所需的参数。这是我第一次使用 Ant 和 mysql,所以语法对我来说是新的。
谁能告诉我如何修复上面的代码,以便它可以找到我的数据库?
编辑:
我听从了威士忌蜘蛛的建议,将我的代码编辑为以下内容:
-- I had a bunch of code here that I am now omitting for brevity because
-- there is a newer, revised version of the code in the second edit below.
这可能会帮助一些读者知道我正在尝试使用 this tutorial 中的代码。 。区别在于我使用的是mysql 5.6版本和版本:Juno Service Release 1 Eclipse 的内部版本号:20120920-0800。本教程使用两个应用程序的旧版本,这就是造成困惑的原因。
第二次编辑:
我再次更改了build.xml文件以实现whiskyspider的第二轮修正。该脚本现在访问 mysql 数据库,但我对下面的另一条消息有疑问。
这是新的 build.xml 代码,现在可以正确运行:
<?xml version="1.0" encoding="UTF-8"?>
<project name="publisher" default="all" basedir=".">
<property name="mysql.params" value="-u publisher -ppublisher -D publisher" />
<property name="mysql.exec.path" value="d:\\mypath\\MySQL\\bin\\"/>
<target name="all" depends="cleandb, createdb, insertdb"></target>
<target name="cleandb">
<exec executable="${mysql.exec.path}/mysqld" input="cleandb.sql">
<arg line="${mysql.params}" />
</exec>
</target>
<target name="createdb">
<exec executable="${mysql.exec.path}/mysqld" input="createdb.sql">
<arg line="${mysql.params}" />
</exec>
</target>
<target name="insertdb">
<exec executable="${mysql.exec.path}/mysqld" input="insertdb.sql">
<arg line="${mysql.params}" />
</exec>
</target>
</project>
以下是运行 build.xml 时 eclipse 控制台中报告的日志:
Buildfile: d:\mypath\workspace\publisher\database\build.xml
createdb:
[exec] Warning: Using a password on the command line interface can be insecure.
insertdb:
[exec] Warning: Using a password on the command line interface can be insecure.
BUILD SUCCESSFUL
Total time: 1 second
当我需要使用密码从使用 eclipse 运行的 ant 代码进入数据库时,谁能告诉我如何通过提高安全性来解决此警告?具体来说,我如何更改我的代码,以便执行相同的任务而不存在安全风险?
最佳答案
实现目标的最佳方法是添加 .../mysql/bin
文件夹到您的PATH
.
如果这不是一个选项,您可以通过属性指定 mysql 位置:-Dmysql.bin.dir=/path/to/mysql
然后使用 <exec executable="${mysql.bin.dir}/mysql" input="cleandb.sql">
关于java - Ant build.xml 导致 java.io.IOException : System cannot find the specified file,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14740585/