简体   繁体   English

在Linux机器上以Java代码运行.sh文件

[英]Run .sh File in Java Code on Linux Machine

My Task: 1 - Read an Excel File, parse it and save the data in txt file. 我的任务: 1-读取Excel文件,对其进行解析并将数据保存在txt文件中。 2 - Read that txt file and pass it to a batch.sh file. 2-读取该txt文件,并将其传递给batch.sh文件。 This batch.sh file do one thing, it picks the data from the above mentioned txt file and save it in a database's table. 这个batch.sh文件做一件事,它从上述txt文件中选择数据并将其保存在数据库表中。

When I run the batch.sh file from the terminal(and give it the txt file), it works fine. 当我从终端运行batch.sh文件(并给它txt文件)时,它工作正常。 It inserts the records in the database just as i want. 它就像我想要的那样将记录插入数据库中。

Problem is , when I want to do the same with the java code, the batch.sh file does not work. 问题是 ,当我想对Java代码执行相同操作时, batch.sh文件不起作用。 And also, no exception is thrown. 而且,不会引发异常。

Some Explanation: I am using Java 7, Oracle, SQL-Loader, Linux. 一些解释:我正在使用Java 7,Oracle,SQL-Loader,Linux。

Additional Information: If I rename the batch.sh file to batch.bat file and run it in Windows environment, it works perfectly fine. 附加信息:如果我将batch.sh文件重命名为batch.bat文件并在Windows环境中运行它,则可以正常工作。 the batch.sh file also works fine if it is executed from the terminal. 如果从终端执行,batch.sh文件也可以正常工作。 The only problem is, its not working from java code. 唯一的问题是,它不适用于Java代码。

I am listing the java code snippet, batch.sh file and the control.txt file for the following task. 我列出了以下任务的Java代码段,batch.sh文件和control.txt文件。

Java Code: Java代码:

try{
    String target = new String("/path/to/batchFile/batch.sh");
    Runtime rt = Runtime.getRuntime();
    Process proc = rt.exec(target);
    proc.waitFor(); 
}catch(Exception e){
    LOG.error("Exception Occured with Message : "+e.getMessage());
}

Batch.sh: Batch.sh:

sqlldr username/password@sid control='/path/to/batchFile/control.txt' log='/path/to/batchFile/Results.log' bad='/path/to/batchFile/BadFile.bad' ERRORS=5000

control.txt: control.txt:

options  ( skip=1 )
load data
  infile               '/path/to/batchFile/TxtFileData.txt'           
  truncate into table   TABLE_NAME
fields terminated by ","       

  (  
  column_name "replace(:column_name,'-','')"    
  ) 

PS: I have read many post regarding the same issue, and tried every solution, but non is working. PS: 我已经阅读了很多有关同一问题的文章,并尝试了每种解决方案,但没有奏效。 The current example of java code is taken from another StackOverFlow thread. Java代码的当前示例来自另一个StackOverFlow线程。

Any help will be highly appreciated. 任何帮助将不胜感激。

您需要runtime.getRuntime.exec(new String[]{"/bin/bash", "-c", "/path/to/script.txt"})请参见此处: 如何执行带参数的命令?

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

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