How to terminate a java thread from c++ code?

Hi,
I made a screensaver which loads a jvm, forks a thread running a java class, wait until user's action(i.e. mouse move/click keyboard input), then terminate the java thread.
However, I met a problem, How to terminate a running java thread from c++ code?
Here is my code, but it does not work: (even after the terminate is called, jvm throws an error)
JNIEnv* env;
JavaVM* jvm;
HANDLE hThread; //handle for the startThread
unsigned __stdcall startThread(void *arg)
     jclass cls;
     jmethodID mainId;
     jint          res;
     int threadNum = (int)arg;
     res = jvm->AttachCurrentThread((void**)&env, NULL);
cls = env->FindClass( MAIN_CLASS);
     mainId = env->GetStaticMethodID(cls, "main", "([Ljava/lang/String;)V");
     // setup the parameters to pass to main()
     jstring str;
     jobjectArray args;
     int i=0;
     args = env->NewObjectArray(1, env->FindClass("java/lang/String"), 0); //only one input parameters
     str = env->NewStringUTF("localhost");
     env->SetObjectArrayElement(args, 0, str);
     env->CallStaticVoidMethod(cls, mainId, args); // call main()      
     if (env->ExceptionOccurred()) {
               env->ExceptionDescribe();
     return TRUE;
Here is the main method:
First create the jvm and load the thread. then tries to terminate the thread, but failed here
switch (msg)
{ case WM_CREATE:
          JavaVMOption options[NUMBEROFOPTIONS];
          JavaVMInitArgs vmargs;     
          jint rc;
          vmargs.version = JNI_VERSION_1_4; /* version 1.4 */
          vmargs.options = options;
          vmargs.nOptions = NUMBEROFOPTIONS;
          vmargs.ignoreUnrecognized = JNI_FALSE;          
          rc=JNI_CreateJavaVM( &jvm, (void **)&env, &vmargs ); /* create JVM */
          /* We pass the thread number as the argument to the invoked thread */
          unsigned int threadId = -1;
          // to initialize a thread-safe C runtime library
          hThread = (HANDLE)_beginthreadex(NULL, 0, &startThread, NULL, 0, &threadId );
} break;
case (WM_DESTROY):
          CloseHandle( hThread );
          jvm->DestroyJavaVM(); /* kill JVM */
          Debug("Destroy Java Virtual Machine\n");
}break;
Note, after the thread "startThread" runs, it has an infinite loop and will not terminate, until the user clicks to terminate.(I didn't call jvm->DetachCurrentThread();_endthreadex(0); in the "startThread" because the java thread will not terminate by itself)
Thus, the only way to terminate the thread is to call closehandle(hthread) in the main thread, which may cause jvm to throw an error.
Do you know how to terminate a java thread safely???
Thanks a lot

Assuming that your java thread is in a loop of some kind. Such as
int i=1; /* I tried using a boolean, I just could not get my C++ env, to change this value. So i decided to use an int */
run {
while(i)
isdfjsdfj
void seti()
i=0
So, B/4, i call destroyVM in my C++ code, i call this seti(). so the loop terminates, therefore my thread finishes executing and ends.
Hope this helps
tola.

Similar Messages

  • How to call a Java class from another java class ??

    Hi ..... can somebody plz tell me
    How to call a Java Class from another Java Class assuming both in the same Package??
    I want to call the entire Java Class  (not any specific method only........I want all the functionalities of that class)
    Please provide me some slotuions!!
    Waiting for some fast replies!!
    Regards
    Smita Mohanty

    Hi Smita,
    you just need to create an object of that class,thats it. Then you will be able to execute each and every method.
    e.g.
    you have developed A.java and B.java, both are in same package.
    in implementaion of B.java
    class B
                A obj = new A();
                 //to access A's methods
                 A.method();
                // to access A's variable
                //either
               A.variable= value.
               //or
               A.setvariable() or A.getvariable()

  • How to invoke a Java Program from Oracle 10g?(uRGENT)

    Hello.
    I've a query, that i have a program, that basically retreives the records from the
    oracle table and then parser this information and then insert the values in corresponding database base tables. I want that, whenever the new program is inserted, a Trigger should fire and pass the most recently entered record to the Parser Program, means
    1) Firing a Trigge
    2)Storing the most latest data and pass it to the Parser PROGRAM
    Can someone tell me how to do this? How to invoke a Java Program from within the database? Please if anyone has examples provide me. Its very urgent and tell me what is the basic mechanism.
    Thankyou.
    Ben

    With Java Stored Procedures Java may be caleed from a database.
    http://www.oracle.com/technology/tech/java/jsp/index.html

  • How to compile a java source from a java program

    Hi .
    I would like to know how to compile my java source from inside an execution of a java program.
    Is there another way than rather use :
    Runtime.getRuntime().exec("javac myFile"); ?

    you can and cann't use the source code of javac... or maybe use jikes...

  • How can we  use java variable in javascript code on JSP page?

    How can we use java variable in javascript code on JSP page?
    Pls help

    Think about it:
    JSP/Java is executed on the Server and produces HTML+JavaScript.
    Then that HTML+JavaScript is transfered to the client. The client now interpretes the HTML+JavaScript.
    Obviously there's no way to access a Java variable directly from JavaScript.
    What you can do, however, is write out some JavaScript that creates a JavaScript variable containing the value of your Java variable.

  • URGENT: How to run a Java program from a different directory?

    Hi.
    How do I run a Java program from a directory that the file is not located in? So lets say im in c:\Java. But the file is in c:\Java\abc\efg\.
    What would be the command to run the Java file from c:\Java.
    I can't remember it and I need it asap.
    Cheers.

    If the class you are trying to run is MyApp.class, try
    c:\Java\>java -cp abc\efg MyAppThe actual classpath you specify will depend on whether or not MyApp.class is in a package (I've assumed it isn't) and whether or not any 3rd party jars are involbed (I've assumed not).
    Edited by: pbrockway2 on Apr 1, 2008 6:42 PM
    The command arguments read as "Run the MyApp class using as a classpath abc\efg relative to here (c:\Java)".

  • (how) can I access Java APIs from web-page-hosted JavaScript?

    Can I access Java APIs from a web-page via JavaScript if I have a JRE installed on the client machine? And if so, how?
    Thanks in advance for your time and consideration.

    John L. wrote:
    Can I access Java APIs from a web-page via JavaScript if I have a JRE installed on the client machine? And if so, how?As far as I can remember that was actually possible in the very first versions of Netscape because Netscape really wanted to pretend Javascript and Java were meant to be used together. But they soon removed that functionality. Can you guess why?
    Because people were using the File/IO classes to easily steal or remove files on the harddrive, among other such niceties. You do NOT want to have such control from a web application. People will abuse it.

  • How to run standalone java file from weblogic server on Solaris

    Hi,
    We have a requirement to run a java file at a scheduled time in a day using cron scheduler on our linux server.
    We need to fetch data from the database & perform some business logic in this standalone JAVA file.
    Our application has an EAR which is deployed on Weblogic 10.3 server & in our application, we are utilizing the datasource created in that domain.
    Now, we have created a standealone Java file & used exisitng datasource (without Hibernate) of the domain with the help of below forums,
    Use DataSource of weblogic in a standalone Java file
    able to achieve the same.
    I've bundled this JAVA in application WAR & depoyed on the same domain where datasource is created.
    Now, how can I execute this file from anywhere on the server using weblogic classpath.
    Please help on the same in implementation.
    Thanks,
    Uttam

    If the Java application must be stand-alone you must not deploy it on WebLogic, then WebLogic will manage its lifecycle.
    In this case you can use the CommonJ API and use the timermanager if it must run on a certain time. More information
    of how to do this can be found here: http://middlewaremagic.com/weblogic/?p=5569
    If you keep the Java application stand-only and want to use a cron job you can follow the example presented here: http://middlewaremagic.com/weblogic/?p=7065
    Note that the example runs a WLST script, but you can follow the same steps to run your Java application.

  • How to terminate all Concurrent Programs from back end

    Hi,
    Can any one tell me how to terminate all Running Concurrent Programs from back end

    post this in the EB forum;
    also, investigate the built-in package fnd_concuurent
    begin
       for r in (select Request_ID from fnd_Concurrent_Requests
                 where Phase_Code != 'C') loop -- exclude requests that have successfully completed (R = Running P = Pending)
         fnd_concurrent.cancel_request(r.Request_ID);
         commit; -- you must perform a commit after each call
       end loop;
    end;
    /

  • How can I extract Java source from Oracle?

    I have an object of type "JAVA SOURCE" in my database. I do not have the source
    code available.
    How can I extract this source from the database so I can read it? I am not a Java programmer, so a sql or pl/sql solution would be greatly appreciated.
    Thanks in advance!

    This query lists any Java object that the current database user has (JAVA SOURCE, JAVA CLASS or JAVA RESOURCE). Use this to determine which Java sources exist.
    SELECT * FROM USER_OBJECTS
    WHERE OBJECT_TYPE LIKE 'JAVA%'This query lists the source of one Java object.
    SELECT * FROM USER_SOURCE
    WHERE TYPE = 'JAVA SOURCE'
    AND NAME = 'com/acme/HelloWorld'Substitue the name of the java class you're interested in for 'com/acme/HelloWorld'.
    I know that the question is four years old, but I had the same question and arrived here via Google, so I figured posting an answer might help others in a similar situation.

  • Ep6.0 How to differentiate a Java Iview  from other Iview

    Hi,
      I would like to know how to recognize a Java Iview in the PCD? I would like to open up an Iview and want to understand whether it's and Java iview or others, I know about bsp, urs, wingui etc..
      Also once I know that, how do I get to see the code behind that Iview, if I need to change etc..
      Also is changing the "cache level" to 'Shared' for bringing in efficiency in the memory use is considered a modification? Currently the 'Cache Level' of some of the iview for one perticular apps is set as 'None'  Is there any other way to do it ?
    Arunava

    Hi Arunava,
    still, the answer stays the same: <i>You cannot determine if the content is provided in Java or in fact in some backend system <b>generically</b> by looking at the properties.</i>
    What does that mean: If you know for some special case that the existance of a certain property means that this iView is served by a BSP for example, then you know this. But in general, a property is a property is a property, and all properties are in first place used by the java implementation - maybe to be passed through to a backend system.
    In my first answer in this thread, I gave complete instructions hwo to find out which java class is called when calling an iView. Always it's in first place a java class.
    Hope it helps to understand why it's impossible to generically determine by properties where the business logic in fact is implemented.
    Best regards
    Detlev

  • How do I remove Java 8 from Yosemite?

    Hi
    I recently installed Java 8 in order to download a specific file.  I no longer need Java and wish to remove it.  I have followed some instructions on the Java website which told me to Trash the Java Applet Plugin - which I've done (even though it wasn't in the folder they said it would be in).  However, I still have a Java Preference Pane showing in Preferences which I can't delete.  When I right-click to remove, it asks for the Admin password and then returns a dialogue saying the item couldn't be deleted because an error occurred.
    Can anyone help with this one please?  I have found other suggestions which advise to remove a Java folder from the /Library/PreferencePanes/ folder but my PreferencePanes folder is empty.
    Thanks for any advice.
    Donna

    I've deleted both the plugin file and the plist file and rebooted.  But I still can't remove the Preference icon in the System Preferences.
    If you can help me get rid of this it would be great.
    Thanks
    Donna

  • How to open a java application from a web page?

    Friends
    I am in a situation to open a java application from a web page. I wrote a applet and placed a button on it. Onclicking of that button i con't open my java application. plz help me to open a userdefined application from web page.
    Ramesh
    e-Brahma technologies

    http://java.sun.com/products/javawebstart/index.jsp

  • How to: Calling a java class from asp?

    Hello all i have a problem to call a Java class from a asp
    Here what I do:
    [JavaSays.java]
    package JavaCom;
    public class JavaSays
    public String Hello()
    return "Hello world" ;
    then
    javareg /register /class:JavaCom.JavaSays /progid:JavaCom.JavaSays
    md c:\winnt\Java\TrustLib\JavaCom
    copy JavaSays.class c:\winnt\Java\TrustLib\JavaCom
    --Asp
    --TestJavaCom.asp
    <html>
    <body>
    <h1>Simple Test</h1>
    <% Set ObjPrueba = Server.CreateObject("JavaCom.JavaSays") %>
    <%= ObjPrueba.SimpleFn(5) %>
    <hr>
    </body>
    </html>
    when i try to run my asp
    it tell me that:
    Error type : Server Objetc, ASP 0177 (0x80040111)
    ClassFactory can not find the class
    any idea???
    thanks.

    I think the OP wants to use a class file as a COM object. I've never done that, but this URL:
    http://support.microsoft.com/default.aspx?scid=KB;EN-US;q167941&
    seems to indicate that you should have placed the class file into the c:\winnt\Java\TrustLib\ sub directory before you ran the JAvaReg bat file - I would re-run JavaReg and bounce IIS and associated services and see if that works out.
    Good Luck
    Lee

  • How to execute a Java program from ODI

    Hi All,
    I am new to ODI. I would like to know whether its possible to execute a java program from ODI.
    Reason for this is, I would like to write information in error tables into a file. And after that another program will mail that error log to concerned person.
    Could anyone please help me with this?
    Thank you in advance
    Regards,
    Srini

    if you have the java code then u can create a procedure with Java BeanShell as the Technology and write your java code in the procedure

Maybe you are looking for