在特定情况下,Java Runtime exec无法加载批处理文件

时间:2016-04-19 14:45:14

标签: java cmd runtime.exec

由于文件夹名称中有括号,此代码失败。 Java Runtime exec无法正确解析文件位置。您有任何建议如何解决这个问题吗?

import java.io.IOException;

public class SampObject {

    String command;

    public void launchBatch() {
        String cmdArray[];

        command = "C:\\test(1)folder\\Launch.bat";

        cmdArray = new String[] {
            "cmd",
            "/c",
            "start",
            command
        };

        try {
            Runtime.getRuntime().exec(cmdArray);
            System.out.println("Batch file launched");
        } catch (IOException e) {
            System.out.println("Error launching file: " + e.toString());
        }
    }
}

公共类示例{

public static void main(String[] args) {
    SampObject sampObj = new SampObject();
    sampObj.launchBatch();
}

}

1 个答案:

答案 0 :(得分:0)

在命令中添加引号:

ruby '2.2.2'
## Sinatra App Gems
gem 'sinatra',      '~> 1.4.4'
gem 'sass',         '~> 3.4.13'
gem 'sinatra-assetpack', '~> 0.3.1', :require => 'sinatra/assetpack'
gem 'sinatra-env',  '~> 0.0.2'

## Rails App Gems
gem 'rails',        '~> 4.0.0'
gem 'foreman',      '~> 0.78.0'
gem 'puma',         '~> 2.14.0'
gem 'simple_form',  '~> 2.1.3'
gem 'simple_enum',  '~> 1.6.0', :require => 'simple_enum/mongoid'
gem 'mongoid',      '~> 5.0.0'
gem 'devise',       '~> 3.4.1'
gem 'possessive',   '~> 1.0.1'
gem 'american_date', '~> 1.1.0'
gem 'sht_rails',    '~> 0.2.2'
gem 'version',      '~> 1.0.0'
gem 'rdiscount',    '~> 2.1.7'
gem 'ssl_enforcer', '~> 0.2.3'
#

gem 'sidekiq',        '~> 3.5.0'
gem 'slim',           '~> 3.0.2'


group :development do
  gem 'capistrano',             '~> 3.4.0'
  gem 'capistrano-rvm',         '~> 0.1.2'
  gem 'capistrano-rails',       '~> 1.1.3'
  gem 'capistrano-bundler',     '~> 1.1.4'
  gem 'capistrano-foreman',     github: 'koenpunt/capistrano-foreman'
  gem 'spring'
  gem 'spring-commands-rspec',  '~> 1.0.4'
end

group :assets do
  gem 'pusher_rails',             '~> 1.0.1'
  gem 'sass-rails',               '~> 3.2.3'
  gem 'coffee-rails',             '~> 3.2.1'
  gem 'jquery-rails',             '~> 3.0.0'
  gem 'bootstrap-sass',           '~> 2.3.0.0'
  gem 'font-awesome-sass-rails',  '~> 3.0.2.2'
  gem 'uglifier',                 '>= 1.0.3'
  gem 'modernizr-rails',          '~> 2.7.1'
  gem 'jquery-payment-rails',     '~> 0.0.1'
  gem 'jquery-validation-rails',  '~> 1.13.1'
end

group :development, :test do
  gem 'test-unit',            '~> 3.0'
  gem 'rspec-rails',          '~> 3.3.3'
  gem 'factory_girl_rails',   '~> 4.5.0'
  gem 'mongoid-rspec',        '~> 1.13.0'
  gem 'guard-rspec',          '~> 4.6.4'
  gem 'simplecov',            '~> 0.10.0', require: false
end

group :test do
  gem 'database_cleaner',     '~> 1.5.1'
  gem 'faker',                '~> 1.5.0'
end