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.

Similar Messages

  • 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");

  • 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 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.

  • 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 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.

  • 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()

  • 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

  • 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 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 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

  • Error in calling 'C'  Program from Java

    Hi,
    We developed a 'C' Program and during compilation, we got a couple of Incompatible Prototype warnings. Then we made a Jni call to 'C' program from Java Program. The program returned a "Unsatisfied Link Error - unreference Symbol not found" error.
    Does Jni call fails owing to warnings by compiler?
    We are using Sun Solaris 2.8.
    Request your assistance as this a urgent issue.
    Thanks
    Srini

    Did you try running your C program through g++? It sounds like something is broken, but GCC often will allow you to compile anyways.
    -Jason Thomas.

  • Problem while Calling a CGI pgm From Java with code sample

    Hey guys,
    I am calling a CGI program from java servlet, while calling cgi program I am passing encoded(Base64) content via post process,
    My problem is the encoded data is not posted as expected, the encoded data is corrupting. But when I send encoded data in a text file, cgi program is perfectly decoding and working fine.
    Here I am doing Base64 encoding as per requirement, I cannot avoid this encoding.
    My doubt is about OutputStreamWriter constructor argument , In OutputStream Constructor I am passing one argument is OutputStream object and another argument is encoding type. I tried with ASCII, US-ASCII & UTF-8 .
    My code is as follows, please help me to resolve this issue.
    URL url = new URL("CGI server path");
    URLConnection urlConnection = url.openConnection();
    urlConnection.setDoOutput(true);
    OutputStream os = urlConnection.getOutputStream();
    BufferedOutputStream buffer = new BufferedOutputStream(os);
    OutputStreamWriter  writer = new
                                                   OutputStreamWriter(buffer, "US-ASCII");
    writer.write(encodedPDF-Content);
    writer.write("\r\n");
    writer.flush();
    writer.close();
    here encodedPDF-Content is String and it's size is 9565 bytes

    Whenever you read something in java into string (with Reader implementation) it expects source to contain text in encoding you specified. It then decodes it and makes 16 bit unicode string from it. Whenever you store string using Writers it does reverse operation with specified encoding (may be different than this which you used to read source) and stores text as a sequence of bytes made of 16 bit unicode string. So, passing text back and forth between programs with the help of files or I/O you can make mistake at both reading and writing encoding. Check for it.
    Now, when C programm or other application reads file it may take another assumptions about encoding or may even completly ignore it and read source as a binary file. Then, if you have a source text as a file and have to pass it to other application never do it using Reader/Writer. User raw InputStream/OutputStream instead what will preserve all information unchanged.
    here encodedPDF-Content is String and it's size is 9565 byteHow id you get this info? String.length() gets you how many chars is in it, it will be half the number of bytes. If you see your input file beeing 9565 bytes long, see my above statements.

  • Calling a CGI pgm From Java

    I am calling a CGI program From Java using URL class and I am passing a pdf file under post process. The Content I am appending by Base64 encoding, the CGI program will decode it and print the content
    When I send this Content the content is correpting. When CGI program is decoding it is not getting expected content. But the CGI program is working fine If I send that encoded content in a text file.
    When I send the encoded content in a text file as attachment OR put in a common drive, the CGI program is working fine. Only I am facing problems when I send this content via post process using URL class.
    Why it is happening like this? Is anybody can help me to Resolve this problem?

    Just a guess: are you sure the content-type header is correct?

  • How to call SAP Webservice in standalone java program

    Hi,
    In our Java application, we want to use the SAP Webservices. I dont know much about authentication mechanism used by SAP. Can any one please help me with any sample code how to Call SAP webservice in Standalone Jave Program. I searched alot on the web regarding this, but helpless. Please help me.
    Thanks,
    Mohan

    Hi Mohan,
    You need an account for the ES Workplace. I'm afraid this is not free, e.g. check [SAP NetWeaver, Composition Subscription|https://www.sdn.sap.com/irj/sdn/subscriptions/composition].
    But I thought you wanted to play with a WSDL [you already had at hand|Sample code to access BAPI Web services from JAVA required;?

Maybe you are looking for