简体   繁体   English

如何在C中运行/控制Java程序?

[英]How can I run/control a Java program from within C?

I am running System Verilog inside of an ASIC simulator. 我在ASIC模拟器中运行System Verilog。 SV has an import/export mechanism to call C functions from SV, and for SV functions to be called from within C. SV具有导入/导出机制,可以从SV调用C函数,并且可以在C内部调用SV函数。

I'd like to send realtime-ish (a very slow stream) data from the simulation to a charting program that I will write in Java. 我想将模拟中的实时数据(非常缓慢的流)发送到我将用Java编写的制图程序中。 What is the best way to call the Java with periodic updates from the simulator/C program? 通过模拟器/ C程序进行定期更新来调用Java的最佳方法是什么?

After a quick glance here : http://java.sun.com/docs/books/jni/html/invoke.html , ... 在这里快速浏览一下: http : //java.sun.com/docs/books/jni/html/invoke.html ,...

Then consider this: 然后考虑一下:

The simplest way is to write the data to a file, and write a java program to read from the file as it grows. 最简单的方法是将数据写入文件,并编写一个Java程序以随着文件的增长从文件中读取数据。 See this answer: Java IO implementation of unix/linux "tail -f" 查看此答案: unix / linux的Java IO实现“ tail -f”

Start them both separately, or use system() to start the java program from your plugin, passing the filename as an argument. 分别启动它们,或使用system()从插件启动Java程序,并将文件名作为参数传递。

The best way would be to have the Java program listen on a TCP socket for updates from the C program which can send them. 最好的方法是让Java程序在TCP套接字上侦听可发送它们的C程序的更新。 Have the C program connect to the Java program when it begins, and whenever there's an update, it can pass it along the connected socket. 让C程序在启动时就连接到Java程序,并且只要有更新,它就可以沿着连接的套接字传递它。 The Java program can then take the data and update whatever it needs to update. 然后,Java程序可以获取数据并更新需要更新的内容。

This also has the nice advantage that the two programs don't even have to run on the same machine. 这还有一个很好的优点,即两个程序甚至不必在同一台计算机上运行。

There is the java native interface which allows C programs to interact with java objects. 有一个Java本机接口 ,它允许C程序与Java对象进行交互。 But you need to write some C code to get this integrated into the ASIC simulator. 但是,您需要编写一些C代码以将其集成到ASIC仿真器中。

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

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