运行终端命令throug java [英] Running terminal commands throug java

查看:170
本文介绍了运行终端命令throug java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须从我的java程序在终端执行两个命令。这里是我使用的java代码:

I have to execute two commands in terminal from my java program. Here is the java code that I am using :

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Runterminal {

public static void main(String[] args) {

Process proc;
Process procRun; 
    String compileCommand = "aarch64-linux-g++ -std=c++14 hello.cpp";
    String runCommand = "aarch64-linux-objdump -d a.out";
try{

     proc = Runtime.getRuntime().exec(compileCommand);
 procRun = Runtime.getRuntime().exec(runCommand);
 // Read the output

    BufferedReader reader =  
          new BufferedReader(new InputStreamReader(proc.getInputStream()));

    String line = "";
    while((line = reader.readLine()) != null) {
        System.out.print(line + "\n");
    }

    proc.waitFor();   

BufferedReader readero =  
          new BufferedReader(new InputStreamReader(procRun.getInputStream()));

    String lineo = "";
    while((lineo = readero.readLine()) != null) {
        System.out.print(lineo + "\n");
    }

    procRun.waitFor();   
}
catch(Exception e)
{
  System.out.println("Exception occurred "+e);
    }

  }
} 

code> hello.cpp 文件存储在主目录中。当我编译的java程序,它得到编译,但在运行它它是抛出异常。这是我得到的输出:

My hello.cpp file is stored in the home directory. When I compile the java program it gets compiled but while running it it is throwing exception. Here's the output which I am getting :

Exception occurred java.io.IOException: Cannot run program "aarch64-
linux-g++": error=2, No such file or directory


推荐答案

尝试在代码中使用以下行替换

Try to replace using following lines in your code,

String compileCommand = "g++ -std=c++14 hello.cpp";
String runCommand = "./a.out";

并保留 .cpp .class 文件所在的目录。

And keep the .cpp file in same directory where the .class file lies.

这篇关于运行终端命令throug java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆