JAVA如何调用Shell脚本
作者:sunshine_kaka 时间:2022-11-25 01:13:13
在实际项目中,Java有时候需要调用C写出来的东西,除了JNI以外,我认为一种比较好的方法是JAVA调用Shell。先把C写出来的make成可执行文件,然后再写一个shell脚本执行该可执行文件,最后是JAVA调用该shell脚本。
JAVA调用很简单,例子如下:
首先是shell脚本
#!/bin/sh
echo Begin word cluster
/home/felven/word2vec/word2vec -train /home/felven/word2vec/resultbig.txt -output /home/felven/word2vec/classes.csv -cbow 0 -size 200 -window 5 -negative 0 -hs 1 -sample 1e-3 -threads 12 -classes 2000
echo The word classes were saved to file classes.csv
然后是JAVA调用代码
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class RunShell {
public static void main(String[] args){
try {
String shpath="/home/felven/word2vec/demo-classes.sh";
Process ps = Runtime.getRuntime().exec(shpath);
ps.waitFor();
BufferedReader br = new BufferedReader(new InputStreamReader(ps.getInputStream()));
StringBuffer sb = new StringBuffer();
String line;
while ((line = brreadLine()) != null) {
sb.append(line).append("\n");
}
String result = sbtoString();
System.out.println(result);
}
catch (Exception e) {
e.printStackTrace();
}
}
}
其实就是一个Process类进行调用,然后把shell的执行结果输出到控制台下。
需要注意的是,在调用时需要执行waitFor()函数,因为shell进程是JAVA进程的子进程,JAVA作为父进程需要等待子进程执行完毕。
另外在eclipse控制台输出时并不是边执行边输出,而是shell全部执行完毕后输出,所以如果执行较为复杂的shell脚本看到没有输出时可能会误以为没有执行,这个时候看看终端里面的进程,TOP命令一下就能看到其实shell脚本已经开始执行了。
来源:http://www.cnblogs.com/xuxiuxiu/p/6266606.html
标签:JAVA,Shell脚本
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
C# 定时器保活机制引起的内存泄露问题解决
2022-09-19 00:31:33
Android中的广播、服务、数据库、通知、包等术语的原理和介绍(图解)
2023-01-20 13:57:52
![](https://img.aspxhome.com/file/2023/0/123250_0s.png)
Java Mybatis框架多表操作与注解开发详解分析
2023-12-04 17:15:14
![](https://img.aspxhome.com/file/2023/6/88746_0s.png)
C#实现根据字节数截取字符串并加上省略号的方法
2021-10-24 22:24:17
C#引用类型和值类型的适用场合和区别
2022-10-20 03:37:25
![](https://img.aspxhome.com/file/2023/3/111183_0s.png)
Android中使用Notification实现状态栏的通知
2022-09-12 00:49:39
![](https://img.aspxhome.com/file/2023/5/137305_0s.gif)
Android应用程序的编译流程及使用Ant编译项目的攻略
2022-09-18 18:22:16
![](https://img.aspxhome.com/file/2023/4/126694_0s.jpg)
Java异常学习之自定义异常详解
2023-09-25 00:57:27
![](https://img.aspxhome.com/file/2023/7/83157_0s.jpg)
Spring中自动装配的4种方式
2021-08-01 16:44:41
Java实现去除文档阴影的示例代码
2023-08-31 11:45:48
![](https://img.aspxhome.com/file/2023/5/58415_0s.png)
java 流与 byte[] 的互转操作
2023-06-26 11:25:46
解决SpringBoot自定义拦截器和跨域配置冲突的问题
2022-01-31 04:59:29
IDEA提高开发效率的7个插件(推荐)
2021-10-16 15:28:35
![](https://img.aspxhome.com/file/2023/0/83410_0s.jpg)
详解SpringBoot中的统一功能处理的实现
2022-07-06 12:14:49
![](https://img.aspxhome.com/file/2023/9/98709_0s.png)
Android adb logcat 命令查看日志详细介绍
2022-10-28 07:42:44
![](https://img.aspxhome.com/file/2023/8/106018_0s.png)
Android Studio 3.5版本JNI生成SO文件详解
2022-06-11 19:34:11
![](https://img.aspxhome.com/file/2023/0/137910_0s.png)
Java同步锁Synchronized底层源码和原理剖析(推荐)
2023-09-25 08:36:22
![](https://img.aspxhome.com/file/2023/1/89951_0s.png)
一文详解如何在控制台显示MyBatis的SQL语句
2023-01-09 06:43:38
![](https://img.aspxhome.com/file/2023/8/99868_0s.png)
Java实现生成JSON字符串的三种方式分享
2022-05-20 15:21:31
Java 8 中 Function 接口使用方法介绍
2022-12-08 23:54:39