File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Calling a javaprogram from C# Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Calling a javaprogram from C#" Watch "Calling a javaprogram from C#" New topic
Author

Calling a javaprogram from C#

des
Greenhorn

Joined: Feb 25, 2005
Posts: 3
Hi,

First of all, I do not develop under C#. But, I have created a java-program that should be called from a C# program.

I know this is possible but the problem is more complex :
- my program is designed to follow the workorders on different machines;
- so, up to 4 machines can be registered in my java-program (in the GUI they are identified by a vertical Tab);
- the C# program should call my java-program with a parameter -> machine;
- and the real problem is => because there are possible 4 machines on the same PC, there can be 4 calls to my javaprogram but I do not want this program to start 4 times. The first time, the java-program may start up (by creating the first vertical tab with the name for the machine) but the other times, there should only be created a new vertical tab and not started a new Java-program!
Is this possible? And if not, do you have any suggestions to avoid this problem.

Thanks in advance for any help.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
A program in another language can call a Java program by several protocols. Your Java program would probably be a "server" which just means a program that starts up, opens some kind of API and waits for connections from clients. Your C# program would be the client, a program that connecxts, sends a request, gets a repsonse, and (optionally) disconnects.

If everything is on the same computer and you have some C/C++ skills, JNI is a choice. Otherwise you can look at raw sockets, HTTP, RMI, etc. It is possible to make clients for these protocols in many languages.

The other half of your problem sounds like internal program design. You receive a call with a machine id on it. If you already have a tab for that id update it. If you don't already have a tab for that machine id, create one.

Did that help? Maybe raise some new questions?


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
des
Greenhorn

Joined: Feb 25, 2005
Posts: 3
Thanks already for your response!
But It seems I didn't explain the problem quiet understandable (it is complex...).

Both, Java and C# program run on one PC. The C# program is in fact the one that "triggers" the java-program. So, the client first launches the C# program, and if selected in this program, the Java-program should be called from within the C# program.
That is achievable but (and this is the complexity !!!) -> the call from C# could be made up to 4 times while the java program may only startup once. So, the first time, the java program starts running but the other times, the java program is already running and I want to be able to capture only the parameter (machine) that is send by the C# program to use in the java-program.

Is this a better explanation?

eds
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
The simplest solution would be to launch the Java program first, maybe in a startup batch file or even as a Windows service. Slightly trickier is to try to make a socket connection from the C# client to a Java serversocket. If the connection fails, the Java program is not running so you start it by C#'s equivalent of exec and then try again. If the connection succeeds the Java program is running and you're in business. Startup is not instantaneous, so if the C# calls might come in very quickly you probably ought to serialize them through a queue or synchronized block of some kind.
des
Greenhorn

Joined: Feb 25, 2005
Posts: 3
Thanks for your advice.

I will try to work around a solution by starting Java first.

Thanks,
Eds
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Calling a javaprogram from C#