Unable to call c++ .so file in java

this is my java file prog1.java
class prog1
     static
          try
               System.loadLibrary("test");
          catch(UnsatisfiedLinkError ule)
               System.out.println(ule);
               //ule.printStackTrace();
     public static native void test();
     public static void main(String args[])
          System.out.println("Hello World!");
          test();
now this is my c++ file prog1.cpp
#include<stdio.h>
#include "prog1.h"
#include<jni.h>
JNIEXPORT void JNICALL Java_prog1_test(JNIEnv *env, jclass jobj)
     printf("Hello world from VC++ DLL\n");
Steps I follow to execute it
1. compile the java code
2. create the header file with javah command
3.compile the c++ code with the command
g++ -c -fPIC -I/usr/local/jdk1.5.0_02/include -I/usr/local/jdk1.5.0_02/include/linux prog1.cpp
4.Now creating the .so file with the command
g++ -shared -O6 -fPIC -I/usr/local/jdk1.5.0_02/include -I/usr/local/jdk1.5.0_02/include/linux -Dlinux -nostdlib -nostartfiles prog1.o -o libtest.so
It generates the error
java.lang.UnsatisfiedLinkError: /root/krgaurav/c++dll/libtest.so: /root/krgaurav/c++dll/libtest.so: undefined symbol: __gxx_personality_v0
Hello World!
Exception in thread "main" java.lang.UnsatisfiedLinkError: test
at prog1.test(Native Method)
at prog1.main(prog1.java:20)
You have new mail in /var/spool/mail/root
Please help .
If I change the file name prog1.cpp to prog1.c it perfectly works .

C++ has a different linkage than C. Function names are changed by the C++ compiler (don't ask me exactly how and why, it's too long ago I was programming in C++, but it's necessary because of certain features of the C++ language).
So if you compile your source as C++, the function names become different from what JNI expects and it won't work.

Similar Messages

  • I need to call a batch file from java and pass arguments to that Batch file

    Hi,
    I need to call a batch file from java and pass arguments to that Batch file.
    For example say: The batch file(test.bat) contains this command: mkdir
    I need to pass the name of the directory to the batch file as an argument from My Java program.
    Runtime.getRuntime().exec("cmd /c start test.bat");
    How to pass argument to the .bat file from Java now ?
    regards,
    Krish
    Edited by: Krish4Java on Oct 17, 2007 2:47 PM

    Hi Turing,
    I am able to pass the argument directly but unable to pass as a String.
    For example:
    Runtime.getRuntime().exec("cmd /c start test.bat sample ");
    When I pass it as a value sample, I am able to receive this value sample in the batch file. Do you know how to pass a String ?
    String s1="sample";
    Runtime.getRuntime().exec("cmd /c start test.bat s1 ");
    s1 gets passed here instead of value sample to the batch file.
    Pls let me know if you have a solution.
    Thanks,
    Krish

  • How to call a .bat file from java code?

    How to call a .bat file from java code? and how can i pass parameters to that .bat file?
    Thanks in advance

    thanks for ur reply
    but still i am getting the same error.
    I am trying to run a .bat file of together tool, my code looks like below
    import java.lang.Runtime;
    import java.lang.Process;
    import java.io.File;
    class SysCall{
         public static void main(String args[]){
              String cmd="D://Borland//Together6.2//bin//Together.bat -script:com.togethersoft.modules.qa.QA -metrics out:D://MySamples//Metrics// -fmt:html D://Borland//Together6.2//samples//java//CashSales//CashSales.tpr";
              //String path="D://Borland//Together6.2//bin//Together.bat ";
              Runtime r= Runtime.getRuntime(); //Declare the system call
              try{
                   System.out.println("Before batch is called");
                   Process p=r.exec(cmd);
                   System.out.println(" Exit value =" + p.exitValue());
                   System.out.println("After batch is called");
              /*can produce errors which must be caught*/
              catch(Exception e) {
                   e.printStackTrace();
                   System.out.println (e.toString());
    I am getting the below exception
    Before batch is called
    java.lang.IllegalThreadStateException: process has not exited
    at java.lang.Win32Process.exitValue(Native Method)
    at SysCall.main(SysCall.java:17)
    java.lang.IllegalThreadStateException: process has not exited

  • Problems with calling a Bat file in java...

    i'm having some issues when i try to call my bat file within java...
    here is the code
    Runtime.getRuntime().exec("cmd.exe C:\\dev\\CMIC\\components\\client\\bin\\startup.bat");
    it starts up the bat file but it seems to only execute the first line in the bat, then it shuts it down... i dont get it. cuz if i call that bat from another bat it works fine.. if i click on it, it works fine why would java have a problem with it?

    After doing some searching around... this is what i came up with.. but i still get the same results.. the dos screen pops up and then poof.. its gone..
    import java.io.BufferedReader;
    import java.io.ByteArrayOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.OutputStream;
    import java.io.PrintWriter;
    public class test {
         public static void main(String Args[]){
         System.out.println("About to try and EXecute the Bat");
                   //String retVal = ExecWindows( "cmd.exe C:\\dev\\CMIC\\components\\client\\bin\\startup.bat" );
                   String retVal = ExecWindows( "cmd.exe /C C:\\dev\\CMIC\\components\\client\\bin\\startup.bat" );
                   System.out.println(retVal);     
         private static String ExecWindows(String cmd) {
              try {
                   ByteArrayOutputStream bos = new ByteArrayOutputStream(100 * 1024);
                   Runtime rt = Runtime.getRuntime();
                   Process proc = rt.exec(cmd);
                   StreamGobbler errorGobbler =
                        new StreamGobbler(proc.getErrorStream(), "ERROR");
                   StreamGobbler outputGobbler =
                        new StreamGobbler(proc.getInputStream(), "OUTPUT", bos);
                   errorGobbler.start();
                   outputGobbler.start();
                   int exitVal = proc.waitFor();
                   //if ( DEBUG ) System.out.println( "cmd: '" + cmd + "' -> " + exitVal );
                   errorGobbler.join();
                   outputGobbler.join();
                   //if (DEBUG && exitVal != 0)
                   //     System.out.println(" ExitValue: " + exitVal);
                   if (exitVal != 0)
                        return "";
                   bos.flush();
                   String output = bos.toString();
                   bos.close();
                   return output;
              } catch (Throwable t) {
                   return "";
         } /* * Handle standard I/O streams */
    } // end of class
         class StreamGobbler extends Thread {
              InputStream is;
              String type;
              OutputStream os;
              StreamGobbler(InputStream is, String type) {
                   this(is, type, null);
              StreamGobbler(InputStream is, String type, OutputStream redirect) {
                   this.is = is;
                   this.type = type;
                   this.os = redirect;
              public void run() {
                   try {
                        PrintWriter pw = null;
                        if (os != null)
                             pw = new PrintWriter(os);
                        InputStreamReader isr = new InputStreamReader(is);
                        BufferedReader br = new BufferedReader(isr);
                        String line = null;
                        while ((line = br.readLine()) != null) {
                             //System.out.println( "{" + line + "}" );
                             if (pw != null) {
                                  pw.println(line);
                             } else {
                                  System.out.println(type + ">" + line);
                        if (pw != null)
                             pw.flush();
                   } catch (IOException ioe) {
                        ioe.printStackTrace();
         }

  • How can i call forpro prg file from java

    Hai friends,
    I have a doubt,clear it.
    how can i call forpro prg file from java file
    by,
    N.Vijay

    Thanks to your reply,
    I have some print statements in my foxpro program file.
    Then i like to invoke that foxpro file from my java file
    This want i want..,
    by,
    N.Vijay

  • Hot to call  forms11g  fmx file  through Java executable

    Hi,
    Iam having one file in some folder which will come through FTP , whenever file comes to the folder i should call the some form fmx file through JAVA so that my form will take care rest of the things .
    My question is how to call fmx through Java executable in windows environment.
    thanks in advance
    GV

    Hi All,
    Thnaks for the Info. But my requirement is different.
    1) whenever i received a file through FTP from other server to my server , some folder say d:\Test
    a) some batch program has to trigger and first it should take the backup of the received file to some other folder say d:\archive
    b) some batch program has to trigger to call the forms fmx file so that form will take care of reading from the file and put the data to database for the received file in d:\Test.
    Assume that it is in windows environment/Linux Environment . How i can achieve these tasks. There should not any user intervention in this , everything is automatic.
    Please guide me any solutions
    Thanks in advance
    GV

  • How to Call a batch file from Java ?

    In my java application I have to call a .bat (Batch file) file.

    I found that already. Eventhough that is slightly differ from yours.
    .exec("cmd /c start x.bat");
    Anyhow
    Thanks yarr.
    My E-Mail ID is [email protected]

  • Calling nested Batch files from Java

    Hi,
    I have a java program which loops through a list of batch files, executing each one. This works fine. My problem is that each batch file I execute contains a call to execute a further batch file.
    The second(nested) batch file never gets run. The java program completes sucessfully though. Any ideas of a way around this?
    Code:
    while (batFileToken.hasMoreTokens()){
        String batFileName = batFileToken.nextToken();
        Process process = load.exec("cmd.exe /c "+batFileName);
        process.waitFor();
    }Each batFileName it executes contains the below command:
    call test.battest.bat never executes.

    Check this link..
    http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html
    the way to make sure process is completed.. is
    try{
    Process proc = Runtime.getRuntime().exec(optimizer.exe);
    StreamGobbler errorGobbler = new StreamGobbler(proc.getErrorStream(), "ERROR");
    StreamGobbler outputGobbler = new StreamGobbler(proc.getInputStream(), "OUTPUT");
    errorGobbler.start();
    outputGobbler.start();
    int exitVal = proc.waitFor();
    System.out.println("ExitValue: " + exitVal);
    catch (IOException ex1) {
    System.out.println("Error in Starting Optimizer");
    catch (Throwable t) {
    t.printStackTrace();
    class StreamGobbler extends Thread {
    InputStream is;
    String type;
    OutputStream os;
    StreamGobbler(InputStream is, String type) {
    this(is, type, null);
    StreamGobbler(InputStream is, String type, OutputStream redirect) {
    this.is = is;
    this.type = type;
    this.os = redirect;
    public void run() {
    try {
    PrintWriter pw = null;
    if (os != null)
    pw = new PrintWriter(os);
    InputStreamReader isr = new InputStreamReader(is);
    BufferedReader br = new BufferedReader(isr);
    String line = null;
    while ( (line = br.readLine()) != null) {
    if (pw != null)
    pw.println(line);
    System.out.println(type + ">" + line);
    if (pw != null)
    pw.flush();
    catch (IOException ioe) {
    ioe.printStackTrace();
    }

  • Unable to generate jra recorder file from java command line.

    Following the instructions at
    http://e-docs.bea.com/wljrockit/docs142/userguide/jra.html#1056021 I'm using
    the following command line to launch my application:
    java -cp bin -Xnoclassgc -Djrockit.lockprofiling -XXjra MyClass
    I'm getting a file lprofile.txt, but no XML file containing the jra
    recording.
    Is the documentation correct?
    jrockit version: BEA WebLogic JRockit(TM) 1.4.2_04 JVM
    Windows XP IA32
    Michael Giroux

    Helena,
    Thanks
    You need to give some parameters to the -XXjra commandNow that I re-read the doc, I see that delay is required. All other
    parameters are optional. I did not notice that the delay has no default
    value. Maybe you could add that to the documentation to make it more clear
    that -XXjra:delay=xx is a minimum requirement.
    Michael
    "Helena Åberg Östlund" <[email protected]> wrote in message
    news:40ff72b4$1@mail...
    Michael,
    You need to give some parameters to the -XXjra command or JRockit will not
    know when to start the recording etc. Parameters are separated by : as
    described in the documentation so the docs are correct. Some valid
    parameters would be for example:
    -XXjra:delay=45,recordingtime=120,filename=myrecording.xml
    which means "start a recording of length 120 seconds in 45 seconds after
    JVM startup. Write the data to the file myrecording.xml".
    I hope this helps.
    /Helena
    Michael Giroux wrote:
    Following the instructions at
    http://e-docs.bea.com/wljrockit/docs142/userguide/jra.html#1056021 I'm
    using the following command line to launch my application:
    java -cp bin -Xnoclassgc -Djrockit.lockprofiling -XXjra MyClass
    I'm getting a file lprofile.txt, but no XML file containing the jra
    recording.
    Is the documentation correct?
    jrockit version: BEA WebLogic JRockit(TM) 1.4.2_04 JVM
    Windows XP IA32
    Michael Giroux

  • How to call .chm help file in java ?

    Hello everybody, at first i am sorry for my bad English. I am doing a project. It request that i have to create a help file for application. But when i create a .chm help file. I can't launch it from my application. Error : Invalid win32 Application. I used Runtime.getRuntime().exec(" file name "); to call .chm file . But it is incorrect. Please help me.
    Thanks in advance.

    do
    Runtime.getRuntime().exec("hh.exe \"file name\"");

  • Calling existing .h file from Java?

    Hi,
    I have a C .h-file that has a method defined as follows: CPXENVptr CPXopenCPLEXdevelop(int *status_p);
    How do I define the function inside Java? The return type and the parameter?

    Read the JNI totorial, and many, many previous psots, about "wrapper" DLLs.

  • Need urgent help. Calling of exe files from java program

    This program can execute small .exe files that donot take inputs but doesn't work for exe files that takes input. what could be the problem.
    Server code :-
    import java.io.*;
    import java.net.*;
    public class Server1 {
         private Player[] players;
         private ServerSocket server;
         private ExecHelper exh;
         String command = null;
         String message = null;
         public Server1() {
              players = new Player[5];
              try {
                   server = new ServerSocket( 12345, 5 );
                   System.out.println("Server started...");
                   System.out.println("Waiting for request...");
              catch( IOException ioe ) {
                   ioe.printStackTrace();
                   System.exit( 1 );
         } //end Server constructor
         public void execute() {
              for( int i = 0; i < players.length; i++ )
              try {
                   players[i] = new Player( server.accept() );
                   players.start();
              catch( IOException ioe ) {
                   ioe.printStackTrace();
                   System.exit( 1 );
         public static void main( String args[] ) {
              Server1 ser = new Server1();
              ser.execute();
              System.exit( 1 );
         private class Player extends Thread {
              private Socket connection;
              private ObjectOutputStream output;
              private ObjectInputStream input;
              public Player( Socket socket ) {
                   connection = socket;
                   try {
                        input = new ObjectInputStream( connection.getInputStream());
                        output = new ObjectOutputStream( connection.getOutputStream());
                        output.flush();
                   catch( IOException ioe ) {
                        ioe.printStackTrace();
                        System.exit( 1 );
              public void run() {
                   try {
                        message = "Enter a command:";
                        output.writeObject( message );
                        output.flush();
                        do {
                             command = ( String ) input.readObject();
                             String osName = System.getProperty( "os.name" );
                             String[] cmd = new String[3];
                             if( osName.equals( "Windows 2000" )) {
                                  cmd[0] = "cmd.exe";
                                  cmd[1] = "/c";
                                  cmd[2] = command;
                             else if( osName.equals( "Windows NT" ) ) {
                             cmd[0] = "cmd.exe" ;
                             cmd[1] = "/C" ;
                             cmd[2] = command ;
                             Runtime rt = Runtime.getRuntime();
                             Process proc = rt.exec( cmd );
                             exh = new ExecHelper( proc, output, input);
                        } while( !command.equals( "TERMINATE" ) );
                   catch( Throwable t ) {
                        t.printStackTrace();
         } //end class Player
         public class ExecHelper implements Runnable {
         private Process process;
         private InputStream pErrorStream;
         private InputStream pInputStream;
         private OutputStream pOutputStream;
         private InputStreamReader isr;
         private InputStreamReader esr;
         private PrintWriter outputWriter;
         private ObjectOutputStream out;
         private ObjectInputStream in;
         private BufferedReader inBuffer;
         private BufferedReader errBuffer;
         private Thread processThread;
         private Thread inReadThread;
         private Thread errReadThread;
         private Thread outWriteThread;
         public ExecHelper( Process p, ObjectOutputStream output, ObjectInputStream input ) {
              process = p;
              pErrorStream = process.getErrorStream();
              pInputStream = process.getInputStream();
              pOutputStream = process.getOutputStream();
              outputWriter = new PrintWriter( pOutputStream, true );
              in = input;
              out = output;
              processThread = new Thread( this );
              inReadThread = new Thread( this );
              errReadThread = new Thread( this );
              outWriteThread = new Thread( this );
              processThread.start();
              inReadThread.start();
              errReadThread.start();
              outWriteThread.start();
         public void processEnded( int exitValue ) {
              try {
                   Thread.sleep( 1000 );
              catch( InterruptedException ie ) {
                   ie.printStackTrace();
         public void processNewInput( String input ) {
              try {
                   out.writeObject( "\n" + input );
                   out.flush();
                   catch( IOException ioe ) {
                   ioe.printStackTrace();
         public void processNewError( String error ) {
              try {
                   out.writeObject( "\n" + error );
                   out.flush();
         catch( IOException ioe ) {
                   ioe.printStackTrace();
         public void println( String output ) {
              outputWriter.println( output + "\n" );
         public void run() {
              if( processThread == Thread.currentThread()) {
                   try {
                        processEnded( process.waitFor());
                   catch( InterruptedException ie ) {
                        ie.printStackTrace();
              else if( inReadThread == Thread.currentThread() ) {
                   try {
                        isr = new InputStreamReader( pInputStream );
                        inBuffer = new BufferedReader( isr );
                        String line = null;
                        while(( line = inBuffer.readLine()) != null ) {
                                  processNewInput( line );
                   catch( IOException ioe ) {
                        ioe.printStackTrace();
              else if( outWriteThread == Thread.currentThread() ) {
                   try {
                        String nline = null;
                        nline = ( String ) in.readObject();
                        println( nline );
                   catch( ClassNotFoundException cnfe ) {
                   //     cnfe.printStackTrace();
                   catch( IOException ioe ) {
                        ioe.printStackTrace();
              else if( errReadThread == Thread.currentThread() ) {
                   try {
                        esr = new InputStreamReader( pErrorStream );
                        errBuffer = new BufferedReader( esr );
                        String nline = null;
                        while(( nline = errBuffer.readLine()) != null ) {
                             processNewError( nline );
                   catch( IOException ioe ) {
                        ioe.printStackTrace();
    Client code :-
    // client.java
    import java.io.*;
    import java.net.*;
    public class Client {
         private ObjectOutputStream output;
         private ObjectInputStream input;
         private String chatServer;
         private String message = "";
         private Socket client;
         public Client( String host ) {
              chatServer = host;
         private void runClient() {
              try {
                   connectToServer();
                   getStreams();
                   processConnection();
              catch( EOFException eofe ) {
                   System.err.println( "Client terminated connection ");
              catch( IOException ioe ) {
                   ioe.printStackTrace();
              finally {
                   closeConnection();
         } //end method runClient
         private void connectToServer() throws IOException {                                                                 
              System.out.println( "Attempting connection...\n");
              client = new Socket( InetAddress.getByName( chatServer ), 12345);
              System.out.println( "Connected to : "+ client.getInetAddress().getHostName());
         private void getStreams() throws IOException {
              output = new ObjectOutputStream( client.getOutputStream());
              output.flush();
              input = new ObjectInputStream( client.getInputStream());
         private void processConnection() throws IOException {
         while( true ){
                   try {
                        message = ( String ) input.readObject();
                        System.out.print( message );
                        InputStreamReader isr = new InputStreamReader( System.in);
                        BufferedReader br = new BufferedReader( isr );
                        String line = null ;
                        line = br.readLine();
                             output.writeObject(line);
                             output.flush();
                   catch( ClassNotFoundException cnfe) {
                        System.out.println( "\nUnknown object type received");
         } //end processConnection
         private void closeConnection() {
              System.out.println( "\nClosing connection");
              try {
                   output.close();
                   input.close();
                   client.close();
              catch( IOException ioe ) {
                   ioe.printStackTrace();
         public static void main( String args[] ) {
              Client c;
              if( args.length == 0 )
                   c = new Client( "127.0.0.1" );
              else
                   c = new Client( args[0] );
              c.runClient();

    maybe you should
    1. Use code tags so the posted code is understandable
    2. Stop marking your post urgent, that just puts peoples backs up
    3. Stop posting the question every couple of hours on every imaginable forum.. Post it once and start a single dialog

  • Calling exe files from java

    heyy
    can anybody please help me out in calling an exe file using java??
    also i would like to pass parameters tooo.
    i have been trying the following runtime code
    but found no success
    Runtime rt = Runtime.getRuntime();
    Process proc = rt.exec("make.exe 12");
    proc.waitFor();
    int exitVal = proc.exitValue();
    Does anybody havea better soultion???
    Thanks for your time
    Rachit

    Read this article, please http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html

  • Running BAT file from Java

    how would i go about calling a batch file in java?
    a button is clicked that says "RUN" and then I want to run a batch file

    Hi !!!
    Im new to this forums but I think I can help you
    You can execute a separate process by using the class Runtime. In this example I execute one batch file located on the same directory the Java App is in.
    class Test
         public static void main(String a[])
              System.out.println("Executing Batch File");
              try{
              Runtime.getRuntime().exec("Runme.bat");
              catch(Exception ex){}
    }I hope that could help
    see
    how would i go about calling a batch file in java?
    a button is clicked that says "RUN" and then I want to
    run a batch file

  • Calling an executable from a java program

    How can I call a compiled program from a java program. I have a fortran program, which I would like to call for execution from within my java program. My OS is linux.
    Thanks,
    An

    Not quite sure in the case of fortran program, but one thing can be done, call ur fortran program from a batch (.bat file) and call this .bat file from java ;
    try {
    Process p = Runtime.getRuntime().exec("run.bat");
    p.waitFor();
    catch( Exception e ) {
    }

Maybe you are looking for