• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

other languages calling java commands

 
Ranch Hand
Posts: 243
Python
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am developing software in Java to communicate with embedded electronic equiptment. I want to allow other developers to support our devices by calling Java methods in my program. How would I go about this?

many thanks
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java has a mechanism for accessing C/C++ called JNI, although I'm not sure if that also allows access from C to Java. A number of other languages can use BSF to integrate with Java in both directions.
 
Ranch Hand
Posts: 53
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!
You could try to do this by using remote method invocation. For example you could use CORBA to access your java objects inside for example your C programm running on the embedded system. I haven't done that before by myself, but I know that there are CORBA orbs for both C and Java and serveral other languages available.

Ernie
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Depending on speed and network requirements, you might find XML-RPC to be an extremely flexible solution. See this web site for more.
This would give you about the most flexible connectivity imaginable.
Bill
My short introduction to XML-RPC article
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ulf Dittmer:
Java has a mechanism for accessing C/C++ called JNI, although I'm not sure if that also allows access from C to Java. A number of other languages can use BSF to integrate with Java in both directions.



Yes you can access Java from a native application by calling the CreateVM(?) function, which returns a pointer into the VM.
 
Ranch Hand
Posts: 547
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
or you could create your own protocol/message format and communicate with the others trough sockets... not the best solution if you have lots of messages/return types but a workaround which also works on limited environments.

pascal
 
Nikos Stavros
Ranch Hand
Posts: 243
Python
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thankd for all your advice

what class provides the CreateVM(?) function?
 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


what class provides the CreateVM(?) function?


It's not from a class. It's an Invocation API function: JNI_CreateJavaVM.
You can statically link this function to your program with the {JDK_HOME}/jvm.lib library.

Regards
 
Nikos Stavros
Ranch Hand
Posts: 243
Python
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do I need to know much about other languages. How much change do I need to make to my exsisting java program?

thanks
 
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The invocation API is quite good, but it's not beginner stuff. I've used it in C/C++. It requires a good working knowledge of those languages, to use it successfully.

You need to determine whether you really need your Java code to be running in the same process as the code in the other language.

If you do, then invocation API is probably the way forward. You could alternatively consider re-architecting your whole system, so that the Java program is the "master", which calls routines (via JNI) in the other languages.

If you don't require the Java and other language code to run in the same process, then it could be easier to run them in separate processes. If performance isn't too much of an issue, they could exchange data via file-passing. If you want better performance and/or reliability than that, you can have the two programs communicate via CORBA, [XML-]RPC, sockets, some new-fangled Web Service or something.
 
Nikos Stavros
Ranch Hand
Posts: 243
Python
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see,

It requires a good working knowledge of those languages, to use it successfully



What I am doing is using Java to communicate with a USB chip (FTDI) which in turn communicates with a pic microprocessor.

I am using Java because it is my only high level language that I am familiar
with.

what I want is the complete system to be supported by external companies if the wish and they would be writting their code in c/c++ or some other lang. I will not be using those languages, I only want them to be able to access my java program, in a similar way to calling say a DLL for something.
 
William Brogden
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How many different kinds of command/response have to be supported? How much memory and CPU time can you devote to communicating to these outside systems.
For example, if there is only one kind of status request and the memory is small, then a custom socket protocol based on sending a byte[] is indicated.
If there are many kinds of command and you have plenty of memory then a more standard protocol such as XML-RPC would be indicated.

Bill
 
Nikos Stavros
Ranch Hand
Posts: 243
Python
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have decided to invoke the VM.

In the following code I get no compile errors



but I do get these when I try and build, I'm using visual studio 6.0



thanks for any advice on how to fix this
 
Peter Chase
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can't statically link the Java Invocation API into your code.

I'm not sure if it's the only way, but in my code, I load the JVM DLL (jvm.dll) at run-time, using the Windows function LoadLibrary(). This requires that your functions knows where the JVM is installed on disk.

Then I find the CreateJavaVM() and GetDefaultJavaInitArgs() functions from the DLL, using the Windows function GetProcAddress(). Look in the MSDN Web site for how to use these functions.

Sounds scary, but isn't actually too hard, as the documentation is good.

There are Mac and Unix equivalents of the above, should you need them.
 
Nikos Stavros
Ranch Hand
Posts: 243
Python
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks, Peter

I'll give it a try
 
Nikos Stavros
Ranch Hand
Posts: 243
Python
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do I load the DLL from the client or the server folder?
 
Peter Chase
Ranch Hand
Posts: 1970
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Nikos Katsikanis:
Do I load the DLL from the client or the server folder?



Your choice. The difference is like the difference caused by using the "-server" switch on the Java command line. Look at Sun documentation for that. In short, server is best for long-running background processes, client is better for interactive applications. Either DLL will run any program, but you may get performance differences.
reply
    Bookmark Topic Watch Topic
  • New Topic