Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
调用编译的C++;来自java的exe文件不工作 我试图从java和C++程序中调用C++程序,如下:< /P> // A hello world program in C++ // hello.cpp #include<iostream> using namespace std; int main() { cout << "Hello World!"; return 0; } //Hello.java public class Hello { public static void main(String []args) { String filePath = "hello.exe"; try { Process p = Runtime.getRuntime().exec(filePath); } catch (Exception e) { e.printStackTrace(); } } }_Java_C++_Mingw_Exe - Fatal编程技术网

调用编译的C++;来自java的exe文件不工作 我试图从java和C++程序中调用C++程序,如下:< /P> // A hello world program in C++ // hello.cpp #include<iostream> using namespace std; int main() { cout << "Hello World!"; return 0; } //Hello.java public class Hello { public static void main(String []args) { String filePath = "hello.exe"; try { Process p = Runtime.getRuntime().exec(filePath); } catch (Exception e) { e.printStackTrace(); } } }

调用编译的C++;来自java的exe文件不工作 我试图从java和C++程序中调用C++程序,如下:< /P> // A hello world program in C++ // hello.cpp #include<iostream> using namespace std; int main() { cout << "Hello World!"; return 0; } //Hello.java public class Hello { public static void main(String []args) { String filePath = "hello.exe"; try { Process p = Runtime.getRuntime().exec(filePath); } catch (Exception e) { e.printStackTrace(); } } },java,c++,mingw,exe,Java,C++,Mingw,Exe,我创建了一个java程序,它应该调用C++编译程序(hello.exe),但是我的java程序正在调用exe,我的程序如下: // A hello world program in C++ // hello.cpp #include<iostream> using namespace std; int main() { cout << "Hello World!"; return 0; } //

我创建了一个java程序,它应该调用C++编译程序(hello.exe),但是我的java程序正在调用exe,我的程序如下:

// A hello world program in C++
// hello.cpp

    #include<iostream>
    using namespace std;

    int main()
    {
        cout << "Hello World!";
        return 0;
    }
//Hello.java

public class Hello {
    public static void main(String []args) {

        String filePath = "hello.exe";
        try {

            Process p = Runtime.getRuntime().exec(filePath);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
} 
检查java程序的输出:

C:\Users\admin\Desktop>javac Hello.java

C:\Users\admin\Desktop>java Hello

C:\Users\admin\Desktop>

为什么不起作用,请帮帮我?

起作用了!!谢谢你们的支持

import java.io.File;
import java.io.InputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Hello {
    public static void main(String []args) {
        String filePath = "hello.exe";
        try {
                ProcessBuilder builder = new ProcessBuilder("hello.exe");
                Process process = builder.start();
                InputStream inputStream = process.getInputStream();
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream), 1);
                String line;
                while ((line = bufferedReader.readLine()) != null) {
                    System.out.println(line);
                }
                inputStream.close();
                bufferedReader.close();
            } catch (Exception ioe) {
                //ioe.printStackTrace();
            }
    }
} 

非常简单,您需要通过
进程
s
输入流
读取进程的输出,例如

String filePath = "hello.exe";
if (new File(filePath).exists()) {
    try {

        ProcessBuilder pb = new ProcessBuilder(filePath);
        pb.redirectError();
        Process p = pb.start();
        InputStream is = p.getInputStream();
        int value = -1;
        while ((value = is.read()) != -1) {
            System.out.print((char) value);
        }

        int exitCode = p.waitFor();

        System.out.println(filePath + " exited with " + exitCode);
    } catch (Exception e) {
        e.printStackTrace();
    }
} else {
    System.err.println(filePath + " does not exist");
}

一般来说,您应该使用
ProcessBuilder
而不是
Process
,它为您提供了更多的选项

尝试从Process获取输入流并读取其内容。您是否可以编辑和发布将很有帮助!!看一看,可能会help@MadProgrammer将尝试buddy,但如果您可以在此处添加这些行,则会有所帮助。我怀疑java进程的工作目录是java.exe所在的位置,而不是桌面,这意味着它在当前目录中找不到hello.exe。尝试在java源代码中使用hello.exe的完整路径。