首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >有没有办法从java中执行Objective-C应用程序?

有没有办法从java中执行Objective-C应用程序?
EN

Stack Overflow用户
提问于 2013-03-05 06:59:08
回答 3查看 167关注 0票数 1

我有一个已经存在的Objective-C控制台应用程序。我不是开发它的人,所以我不容易访问代码。因此,更改代码可能不是一种选择,但我所知道的是,为了进行编译,它需要Cocoa和SystemConfiguration。(我在一封电子邮件中被告知)一旦运行,它会有一个等待命令的提示符,并且在该命令之后会有一个文本输出结果。有没有一种方法可以让我在java中运行这个应用程序并捕获输出?

我以前从来没有做过OSX开发,但我知道C代码可以很好地与Java一起工作,Obj-c是C的超集,但根据框架要求,似乎很明显在代码中的某个地方使用了对象。

我见过像rococoa这样的东西,但已经有一段时间没有更新了(2009),它还可以和美洲狮一起工作吗?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-03-05 07:18:55

当你说你有一个Objective-C应用程序时,你的意思是你有一个为Mac编译的二进制/可执行文件?

如果是这样的话,您可以使用Class ProcessBuilder来创建操作系统进程。

代码语言:javascript
复制
Process p = new ProcessBuilder("myCommand", "myArg").start();

注意:此解决方案只能在Mac上运行。此外,您可能会遇到一些安全问题,因为Java喜欢在沙箱中运行。

票数 2
EN

Stack Overflow用户

发布于 2013-03-05 07:24:29

如果你想创建子进程并在它和你的主程序之间交换信息,我认为下面的代码将会对你有所帮助。

它通过调用二进制/可执行文件来创建子进程,然后编写一些内容(例如,命令)添加到其输入流,并读取一些文本:

代码语言:javascript
复制
import java.io.*;


public class Call_program
{
    public static void main(String args[])
    {
        Process the_process = null;
        BufferedReader in_stream = null;
        BufferedWriter out_stream = null;

        try {
            the_process = Runtime.getRuntime().exec("...");   //replace "..." by the path of the program that you want to call
        }
        catch (IOException ex) {
            System.err.println("error on exec() method");
            ex.printStackTrace();  
        }

        // write to the called program's standard input stream
        try
        {
            out_stream = new BufferedWriter(new OutputStreamWriter(the_process.getOutputStream()));
            out_stream.write("...");     //replace "..." by the command that the program is waiting for
        }
        catch(IOException ex)
        {
            System.err.println("error on out_stream.write()");
            ex.printStackTrace();  
        }

        //read from the called program's standard output stream
        try {
            in_stream = new BufferedReader(new InputStreamReader(the_process.getInputStream()));
            System.out.println(in_stream.readLine());   //you can repeat this line until you find an EOF or an exit code.
        }
        catch (IOException ex) {
            System.err.println("error when trying to read a line from in_stream");
            ex.printStackTrace();  
        }
    }
}

参考

http://publib.boulder.ibm.com/infocenter/iseries/v5r4/index.jsp?topic=%2frzaha%2fiostrmex.htm

票数 1
EN

Stack Overflow用户

发布于 2013-03-05 07:06:00

如果这个列表是全面的,那么就不存在纯粹的Objective-C解决方案。

http://en.wikipedia.org/wiki/List_of_JVM_languages

您的下一个选择是从Java调用C的解决方案-这很容易谷歌本身,所以我不会在这里包含信息-或者更多的解耦解决方案(这很可能是您想要的),例如使用消息总线,如ZeroMQ或RabbitMQ。

我强烈建议避免桥接两种语言,除非您有特别不寻常的体系结构或硬件需求。桥接两种语言需要O(n^2)个API来学习语言的数量,实现消息队列是O(n)。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15212797

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档