This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HELP: Launching JVM from Visual C++ problem

 
Fabien Tronche
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey there,
Problem: I'm trying to launch a .jar file from a Visual C++ project. I've got a MFC application with a standard CDialog. When I click a button on this dialog, I invoke the JVM using JNI, passing all the proper arguments (I think).
Result: The Java app contained in the .jar file starts properly but the processing doesn't come back to my C++ application. And when I close the Java app, the C++ app crashes.
I've tried calling jvm->DetachCurrentThread(); defore to destroy the jvm but with no success.
Please help me on this, I'm going completly insane I can provide source code if it can help...
Thanks in advance,
Fabien.
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Fabien,
A little more information would be helpful here. What exactly are you trying to accomplish? Do you want the Java app to be detached from the native app or do you want to control/communicate with the Java app from the native app or vice-versa?
Michael Morris
 
Fabien Tronche
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Michael,
Thanks for the reply. Here is what I'd like to do:
I have a MFC application that:
1 - Displays a splash screen
2 - Start the JVM and launch the .jar file
3 - Hides the spalsh screen
4 - Exits
I want the java app to be detached, so that it can still run even after the MFC app quits. I don't need any communication between the two.
Thanks,
Fabien.
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Fabien,
Are you starting the Java app, with CreateProcess()? If not, how?
Michael Morris
 
Fabien Tronche
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael,
I'm starting the java app using the JNI methods (JNI_CreateJavaVM, ...).
That's where the problem is, i can create the Java VM, call the main method inside the Main-class of the .jar file.
This starts the java app, but then it seems that I can't detached the Java VM from the current process (C++ app).
Whether I would like to finish the C++ app and still have the java app running normally.
Thanks,
Fabien.
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Fabien,
You might try writing a small bootstrap program that launches the JVM and call it from your MFC app with CreateProcess() istead of launching the JVM directly from the MFC app. It's been a while since I did any C++ programming in Windows and I can't remember if the child process can run after the parent exits when calling CreateProcess(). In Unix, you can detach an external process with fork(). You may be able to do that with Windows' spawn().
Hope this helps,
Michael Morris
Michael Morris
 
Fabien Tronche
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Michael,
Thanks for the help, I think I will use CreateProcess for now and use the command line call but in the future I'd still like to use the JNI functions. It seems that there are some problem with message processing in the C++ code when calling the JNI...
I'll keep looking and I'll post any solution I can find...
Thanks again for the help, much appreciated!
Fabien.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic