Call a local program in Java class on Win

Hi
I'm now working on a Java project. It is needed to call a local program in Java class.
Following is the local program's source code:
#include <stdio.h>
main()
    char s[100];
    printf("Please input\n");
    scanf("%s", s);
    printf("Output: %s\n", s);
} I build this simlpe C program to a exe file named TEST.EXE
Then, I called the exe in my java class which was like following:
final Process p = Runtime.getRuntime().exec("test.exe > a.txt");
writer = new PrintWriter(p.getOutputStream());
writer.println("12345\n");
writer.close();
p.waitFor();But after I ran the Java, it seemed not what I hope to get in a.txt. the string "12345" had not been read by the program, and not been writen to a.txt.
Following is what appeard in a.txt:
Please input!
Output: Is there any error in my Java code?
thanks!
Edited by: JingZ on Aug 20, 2008 9:05 AM

All above is my testing.
In fact, my project is to encrypt/decrypt by Gnu PG.
I need to execute a gpg command like following
gpg -r Luna --encrypt test.txt
I could execute this command line in CMD normaly:
C:\WP_ECL~1.4\TESTCE~1>gpg -r 8104EFB8 --encrypt test.txt
gpg: 997D0744: There is no assurance this key belongs to the named user
pub  4096g/997D0744 2002-07-31 Baker &Taylor Inc. <[email protected]>
Primary key fingerprint: BBBC F196 C1D5 CF26 170F  D1FB 870A 3F60 8104 EFB8
      Subkey fingerprint: C161 4C33 F15C 5347 FD09  C2BE 8CC6 2302 997D 0744
It is NOT certain that the key belongs to the person named
in the user ID.  If you *really* know what you are doing,
you may answer the next question with yes.
Use this key anyway? (y/N) y
C:\WP_ECL~1.4\TESTCE~1>Please pay attention to the line which I have to type a 'y' to confirm.
This make me have to invoke this command in Java class like following(in my understanding):
final Process p = Runtime.getRuntime().exec("cmd /C gpg -r Luna --encrypt test.txt");
writer = new PrintWriter(p.getOutputStream());
writer.println("y");
writer.close();
p.waitFor();But while the java code was being run, it would be blocked, waiting, waiting and waiting.
But this method was succeed in my test which invoked test.exe above.
I cannot understand what difference exists between my test.exe and gpg.
thanks.

Similar Messages

  • Calling Same C program from Java Classes

    Hi!
    Can anyone let me know how to call the same C program using JNI from multiple Java Classes/programs?.
    Cheers
    Sai

    JNI creates a shared library.
    Any application that needs to can use the shared library at the same time.
    Ideally you use the same interface that you created for one application via JNI in the other applications.

  • How do I call a C++ program from Java?

    I've tried using Runtime.exec to call my C++ program from Java but I don't think I'm doing it correctly. Could someone explain how to properly use it? Here's what I'm trying (this is in an applet that is only going to be running on an intranet so only PCs in my LAN will be affected):
    Runtime runtime=runtime.getRuntime();
    Process proc =runtime.exec("mkdir C:\\Test");

    What do you expect your operating system should do with a command "mkdir"? It doesn't understand this. "mkdir" is a shell command, so you need to execute the shell:
    runtime.exec("cmd /c mkdir C:\\test");

  • Call a c program in java

    i am trying to call a c program in java,can anyone tell me how ot do this using jni

    That has nothing to do with JNI nor java.
    However if you mean an executable, then the will be an OS call, specific to your OS, which allows you to run a process/application. That is the only way you can do it.

  • How to Call abap functn/program from java layer

    Hi all,
        I have to develop a program which has to call abap function/program from java side or how to call a abap program through java ..
    pls send me related links or explanations.. dont send unrelated answers..
    Regards,
    Arivarasu S

    Hi,
    You mean accessing ABAP functions from J2EE perspective, then I think we can do this by using SAP Java Resource Adapter and also through webservices.
    SAP Java Resource Adapter (SAP JRA) can be used as an add-on for the SAP JCo SAP JRA enables the implementation of standard interfaces from diverse J2EE servers to the SAP JCo in the SAP Web AS. The SAP JRA thus simplifies
    communication with ABAP within heterogeneous J2EE landscapes.
    Go through the following links which has Good documentation on how to achieve this
    Accessing BAPIs Using the SAP Java Resource Adapter
    [https://www.sdn.sap.com/irj/sdn/go/portal/prtroot/docs/library/uuid/ad09cd07-0a01-0010-93a9-933e247d3ba4]
    Connectivity and Interoperability
    [https://www.sdn.sap.com/irj/sdn/go/portal/prtroot/docs/library/uuid/326d82e5-0601-0010-fca4-9caf27b89c26]
    Finally with WebServices. Accessing SAP Business Functions (ABAP) via Web Services
    [https://www.sdn.sap.com/irj/sdn/go/portal/prtroot/docs/library/uuid/06adbf03-0a01-0010-f386-d8e45561a3c4]
    Regards
    Raghu

  • Call visual prolog program from java

    Hi friends,
    is there someone help me to call ( run) prolog program from java.
    i write a parser with Javacc parser generator and stored parsed information in prolog database and I want create some query in java that work in the prolog file.
    how can i combine java and prolog programs. i used visual prolog.
    Can someone help me?
    Thanks you in advance.
    yours sincerely,
    ksu

    Since visual prolog can produce dll's, you can use JNI:
    http://java.sun.com/j2se/1.4.2/docs/guide/jni/index.html

  • How to call a C program in java?

    I would like to call a C program (which does not return anything back to the Java program) from a Java class? Or is there any simple example just to do such a task online?
    Thanks.

    two options:
    1) Use JNI and call your C routines directly
    2) Make an executable and call it using Runtime.exec()

  • How to call a external program in java?

    Help!!
    Is there any method that can a java program can call a external program? For example execute a exe file.
    Thanks.

    Yes.
    Runtime.getRuntime().exec("exactly what you would type at the command line");
    But be aware that this is operating-system-specific and full of gotchas. When you run into one of them, come back to the forum and do a search, this is a frequent topic of discussion.

  • Calling a servlet thru'  a Java class

    Hi,
    How can I write a Java class that calls/invokes a servlet?
    Thnks

    I blv you can do some thing like this to invoke a servlet
    URL test = new URL(myURL);
    where myURL should contain the complete URL of your servlet
    test.openStream();
    check more docs on the URL class

  • Calling OAM WLST Commands from java class

    Hi all,
    is there any idea how to call OAM related WLST commands from java class ?.
    what are the required jar files ?
    thanks

    Hi,
    As per my understanding in OAM you will have only two major .py file startscript.py and stopscript.py file which will start nodemanger connect and start Admin and managed server similarly in stop it will stop managed server Admin server and then last nodemanager.
    these all done through wlst command using nmConnect(), nmStart (), nmKill() and shutdown etc.
    What exactly you are looking to get from java code.
    Regards,
    kal

  • Calling a C program from Java

    is there a way we can supply those command arguments when say we click a button, and the command argument is somthing like
    "server test.txt output.txt"
    where server is an executable ".exe" written in C and test.txt is the input file while output.txt is the output file..
    bottom line... can java call a C program within a GUI ??
    is that possible?
    is it the only way through: Runtime.exec() ?
    thank you very much

    my c program is an exe..
    i tried to execute the program using runtime.exec but faced problem..please help..
    im using the StreamGobbler from: http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html
    try
    String fileinputstring = "./outputfile/input.dat";
    FileInputStream fis = new FileInputStream(fileinputstring);
    FileOutputStream fos = new FileOutputStream("./out/test/myfile");
    Runtime rt = Runtime.getRuntime();
    Process proc = rt.exec("./out/server.exe " + fileinputstring);
    StreamGobbler errorGobbler = new StreamGobbler(proc.getErrorStream(), "ERR", null);
    StreamGobbler outputGobbler = new StreamGobbler(proc.getInputStream(), "OUT", fos);
    errorGobbler.start();
    outputGobbler.start();
    int exitVal = proc.waitFor();
    System.out.println("Exit Value: " + exitVal);
    fis.close();
    fos.flush();
    fos.close();
    catch(Throwable t)
         t.printStackTrace();
    }basically if my c program is to be run from the command prompt it would be:
    server outputfile/input.dat out/test/myfile
    my problem is that the file in out/test/myfile should contained the output that is generated from server...
    my code actually correctly print out the output i wan to see on screen but doesn't write the c output onto "myfile"...so i wonder whether i executed my program correctly this way..
    please help...thanks

  • How to call a C program in java PACKAGE

    am trying to call a method implemented in C from java.
    I followed the instructions of sun.java.com tutorial for the simple example of HelloWorld. It works fine.
    BUT, when I try to do the same in a package, I run into a lot of trouble. I did the following :
    1. Created HelloWorld.java in a package GUI/ and compiled it
    package GUI;
    import java.io.*;
    class HelloWorld {
    public native void displayHelloWorld();
    static {
    System.loadLibrary("hello");
    // Call native method through this from other classes
    public void callinghello() {
    callip();
    public static void main(String[] args) {
    new HelloWorld().displayHelloWorld();
    2. javah -jni HelloWorld -- to create HelloWorld.h
    3. wrote HelloWorldImp.c
    4. g++ -Wall -shared -I/usr/lib/j2sdk1.3/include -I/usr/lib/j2sdk1.3/include/linux HelloWorldImp.c -o libhello.so
    -- compiled and created .so file
    5. Set the package directory in Classpath, path and LD_LIBRARY_PATH
    6. When it is run java GUI.HelloWorld
    I get this error
    Exception in thread "main" java.lang.NoClassDefFoundError: OrionGUI/HelloWorld
    Can anyone help me. If you have any links to how to call native methods in package, please let me know. Thanks.

    GOT IT !!!
    I had to change the name of the method in .h file generated by javah command. On doing
    javac -d ../../classes HelloWorld.java
    go to the ../../classes directory (where you have the class file) and do
    javah HelloWorld
    I got a HelloWorld.h file in which I had
    JNIEXPORT void JNICALL Java_HelloWorld_display(JNIEnv *, jobject);
    I added the package name too:
    JNIEXPORT void JNICALL Java_GUI_HelloWorld_display(JNIEnv *, jobject);
    The HelloWorldImp.c file should have the same name (ie with package) and be in the same directory(ie ../../classes)
    compile and build the shared library to get "libhello.so" file
    gcc -c -fPIC -I/usr/lib/j2sdk1.3/include -I/usr/lib/j2sdk1.3/include/linux HelloWorldImp.c
    gives .o file
    gcc -shared -o libhello.so HelloWorldImp.o
    gives .so file
    then run java with the command in my first message. It works.
    Thanks for the reply "thedracle".

  • How choose between post and get when calling a servlet from a java class ??

    Hi !!
    I have an urgent question : in fact some java programs that call servlets are published on the web, and the principle is to make an url.openconnection() to reach the servlet.
    What I don't understand is how we choose if we want to call the get or the post method on the servlet ?????
    please answer very fast.....
    thanks...

    from the URL documentation of openConnection():
    "If for the URL's protocol (such as HTTP or JAR), there exists a public, specialized URLConnection subclass belonging to one of the following packages or one of their subpackages: java.lang, java.io, java.util, java.net, the connection returned will be of that subclass. For example, for HTTP an HttpURLConnection will be returned, and for JAR a JarURLConnection will be returned."
    Obviously, in case of a http servlet, normally an instance of HttpURLConnection will be returned. If you don't change the request method, GET is the default.

  • How to call a external program in java program

    for example, if I want to execute internet explore or windows word in java program, how to do it?

    http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Runtime.html

  • Integrate VB program in Java class

    we are having VB function which solves typical purpose. We can't touch that VB program. But we can call that function. May we know the solution for this problem. Thanks in advance

    look for jintegra
    it may help

Maybe you are looking for