0

I have a binary executable that I do have source code in c++. I need to use it in certain ways in my Java code. I am planing to wrap it as a class in Java, so my code will call my class instead of calling the executable directly. This is similar to how .net wraps old com component. Since I am new to java, I am wondering if there is better way to solve it.

Frank
  • 7,235
  • 9
  • 46
  • 56
  • By *"binary executable"* DYM A Jar with classes? – Andrew Thompson Jun 05 '12 at 17:41
  • you want to wrap it as a class. Java Bytecode? Or what type of execution you mean? – timaschew Jun 05 '12 at 17:42
  • To elaborate on Andrew's question, is the executable some java .jar files, or a compiled C/C++ etc. file (e.g., on Windows, an .exe file) – user949300 Jun 05 '12 at 17:55
  • Actually I found its c++ source code. – Frank Jun 05 '12 at 18:06
  • 1
    There is `Java Native Interface (JNI)` which is created for calling native code (mostly written in C++) in Java. Can you modify the C++ code? I believe you shall modify it somehow (in case of arguments, or adding some compiler directives) to call it with JNI. By the way look at libraries like [Java Native Objects](http://www.innowhere.com/webapp/index.jsp#!st=products.jnieasy) which simplify JNI. – Amir Pashazadeh Jun 05 '12 at 18:14

1 Answers1

1

It depends on the "certain ways" you want to use it.

If you want to call methods directly, you (probably) need to learn JNI. There are some weird COM <-> Java things, or at least there used to be.

If you want to more or less "pipe" data into and out of the executable, similar to UNIX commands, you need to look at Runtime.exec(), or the newer (and better) ProcessBuilder. A direct use of Runtime.exec() is challenging, so, if this is what you want, search for some libraries that help support it, or for example code.

(EDIT ADDED in response to OP's comment) For running as a shell command, you'll want to look at RUntime.exec() or ProcessBuilder. Here are some links:

stack overflow discussion 1

stack overflow discussion 2

A JAvaWorld discussion of some pitfalls here

Community
  • 1
  • 1
user949300
  • 15,364
  • 7
  • 35
  • 66