java - Ant build.xml 导致 java.io.IOException : System cannot find the specified file

标签 java mysql eclipse ant

当我尝试运行以下 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/

相关文章:

javax.ws.rs.NotFoundException : Could not find resource for full path: with RESTEasy, Eclipse Luna 和 Tomcat 7

java - 在 eclipse 中使用 ObjectAid

android - 我应该将图像放在 eclipse 资源文件夹中的什么位置?

java - 将图像插入 vaadin 网格

java - 如何使用 Guava 从任何方法获取返回类型?

java - 第二次解析 XML 文件时出现 XStream ConversionException

mysql - 在Codeigniter中添加mysql表之间的关系

mysql - 我可以为 MySQL 客户端中的所有选择设置默认 LIMIT 吗?

java - 在Java中读取图像文件

php - MySQL 5.5.47 表从 MyISAM 切换到 InnoDB