简体   繁体   English

C++ 转 Java(openmp)

[英]C++ to Java(openmp)

I'd appreciate it if you could help on this如果您能对此提供帮助,我将不胜感激

Basically what I need to do is to find the best way to load a cpp file through java.基本上我需要做的是找到通过java加载cpp文件的最佳方法。

Explaining more, I have a program written in C++ (openmp) and I need to write a Java Gui that will run this file along with some other tasks.解释更多,我有一个用 C++ (openmp) 编写的程序,我需要编写一个 Java Gui 来运行这个文件以及其他一些任务。

What is the most efficient and easier way to do this?最有效和最简单的方法是什么? Do you have any online books, or recommendations for it?你有任何在线书籍或推荐吗?

Also could this be done through xml?这也可以通过 xml 来完成吗? I mean have the xml structure and load the java's gui file and then the.cpp?我的意思是有 xml 结构并加载java的gui文件然后.cpp? How could this work?这怎么能行?

Thanks谢谢

I think what you are looking for is the JNI: http://en.wikipedia.org/wiki/Java_Native_Interface我认为您正在寻找的是 JNI: http://en.wikipedia.org/wiki/Java_Native_Interface

Search the web for "JNI getting started" or "JNI tutorial" or "JNI Hello World".在 web 中搜索“JNI 入门”或“JNI 教程”或“JNI Hello World”。

Take a look at Runtime.getRuntime().exec(String);看看Runtime.getRuntime().exec(String); . . This method invokes another application.此方法调用另一个应用程序。 Here is some example usage:这是一些示例用法:

public void runBtnActionPerformed(ActionEvent evt)
{
    try {
        Process p = Runtime.getRuntime().exec("./mycppapp"); // or for Windows "mycppapp.exe"
        BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
        String line = null;
        while ((line = br.readLine()) != null)
        {
            System.out.println(line);
        }
    } catch (Exception e)
    {
        // handle here
    }
}

Be sure you compiled your C++ application.确保您编译了 C++ 应用程序。 It is impossible to run the code without compilation.没有编译就不可能运行代码。 You can try to compile the C++ through Java, using the same method:您可以尝试通过 Java 编译 C++,使用相同的方法:

int success = Runtime.getRuntime().exec(new String[]{"g++", "mycode.cpp"}).waitFor();

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

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