Decision Optimization

Decision Optimization

Delivers prescriptive analytics capabilities and decision intelligence to improve decision-making.


#Analytics
#DecisionOptimization
#DecisionOptimization
 View Only
Expand all | Collapse all

IloOplJavaCall Inside CPLEX IDE

  • 1.  IloOplJavaCall Inside CPLEX IDE

    Posted 04/06/16 04:05 PM

    Originally posted by: mathygirl


    I'm running a jar file inside an OPL Script main block. In particular, I'm running a method, call it "myMethod" that takes as input a String object which is a directory of files to be processed by "myMethod". I have an OPL Script runner model, myExternalJavaCall.mod, formulated as follows:

    main 

     IloOplImportJava("myJar.jar");    
     IloOplCallJava("myPackage.myClass", "myMethod", "", "pathToDataDirectory"); 
    }

     

    I can successfully run the jar using the OPL Script runner model with the following command line invocation in Windows Powershell:

    oplrun .\myExternalJavaCall.mod

    However, when I try to run this same model in the OPL Studio IDE, I get

    IloOplCallJava: java.lang.NullPointerException

    I've tried adjusting the path to the data directory, which is the input to "myMethod", but it hasn't been working.

    How can I get myExternalJavaCall.mod working in the IDE and not just by invoking oplrun from the command line?

     

     


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 2.  Re: IloOplJavaCall Inside CPLEX IDE

    Posted 04/07/16 02:52 AM

    Hi,

    you have an example at CPLEX_Studio1263\opl\examples\opl_interfaces\java\externaldataread

    This example both works with oplrun and within the IDE

    var customDataSource = IloOplCallJava("externaldataread.ExternalDataRead",
          "<init>", "(Lilog.opl.IloOplModel;)V", opl);
           

    Regards


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 3.  Re: IloOplJavaCall Inside CPLEX IDE

    Posted 04/07/16 11:25 AM

    Originally posted by: mathygirl


    This example works for me in the IDE as well. The difference is that myMethod takes as input a path to a data directory. I'm wondering if there are some IDE path settings that I need to set manually.

    I need to pass in something like

    string input = "C:\\dataDirectoryName";

    main {

        thisOplModel.generate();

        IloOplImportJava("myJar.jar");

        IloOplCallJava("myPackage.myClass", "myMethod", "", thisOplModel.input);

    }

     

    This script works from the command line, but not in the IDE.

     

    All the examples that do IloOplCallJava work in the IDE. The difference between those examples and what I need is that I'm passing a directory string to my function. The IDE is not able to pick up the directory string for some reason.


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 4.  Re: IloOplJavaCall Inside CPLEX IDE

    Posted 04/07/16 11:46 AM

    Hi,

    is this path a relative or absolute path ?

    regards


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 5.  Re: IloOplJavaCall Inside CPLEX IDE

    Posted 04/07/16 11:51 AM

    Originally posted by: mathygirl


    I've tried both a relative path and an absolute path. 

    When I run from the command line, both relative and absolute paths to the data directory work.

    The relative path is taken with respect to the directory where the model that contains the "main" block is run.

    So, if I place the data directory, "data",  in the same folder where the model is located, the following working from the command line:

     

    string input = "data";

    main {

        thisOplModel.generate();

        IloOplImportJava("myJar.jar");

        IloOplCallJava("myPackage.myClass", "myMethod", "", thisOplModel.input);

    }

     

    The absolute path also works from the command line:

     

    string input = "C:\\<pathToModelDir>\\data";

    main {

        thisOplModel.generate();

        IloOplImportJava("myJar.jar");

        IloOplCallJava("myPackage.myClass", "myMethod", "", thisOplModel.input);

    }

     

    This same code doesn't work in the IDE.

     


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 6.  Re: IloOplJavaCall Inside CPLEX IDE

    Posted 04/08/16 07:11 AM

    do you have any space character in your path ?


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 7.  Re: IloOplJavaCall Inside CPLEX IDE

    Posted 04/08/16 07:50 AM

    Hi

    if you still have the issue, you should open a PMR at http://www-01.ibm.com/support/docview.wss?uid=swg21507639

    regards


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 8.  Re: IloOplJavaCall Inside CPLEX IDE

    Posted 04/08/16 09:44 AM

    Originally posted by: mathygirl


    Yes, there are space characters. Again, those are not an issue when I run from the command line.


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 9.  Re: IloOplJavaCall Inside CPLEX IDE

    Posted 04/08/16 09:50 AM

    Hi,

    can you try to change the names of the folders so that you do not have those space characters ?

    They could be guilty.

    regards


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 10.  Re: IloOplJavaCall Inside CPLEX IDE

    Posted 04/08/16 09:54 AM

    Originally posted by: mathygirl


    Actually, there are no space characters in the folder names I am using. 


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 11.  Re: IloOplJavaCall Inside CPLEX IDE

    Posted 04/08/16 09:59 AM

    Originally posted by: mathygirl


    I am getting this error message:

     

    Execution of "main" failed: "<pathToDirectory>\callJar.mod", line 9: IloOplCallJava: java.lang.NullPointerException -> <myPackage>.myClass.myMethod(myClass.java:978) -> at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) -> at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:76) -> at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) -> at java.lang.reflect.Method.invoke(Method.java:607) -> at ilog.opl.IloOplExternalCall.callStaticJava(IloOplExternalCall.java:153) -> at ilog.opl.opl_lang_wrapJNI.IloOplModel_main(Native Method) -> at ilog.opl.IloOplModel.main(IloOplModel.java:652) -> at ilog.odms.ide.opllang.remote.IloOplRunConfigRunner.doProcessMain(IloOplRunConfigRunner.java:359) -> at ilog.odms.ide.opllang.remote.IloOplRunConfigRunner.doProcessModel(IloOplRunConfigRunner.java:167) -> at ilog.odms.ide.opllang.remote.IloOplRunConfigRunner.doRun(IloOplRunConfigRunner.java:398) -> at ilog.odms.ide.opllang.run.IloAbstractRunShell$2.run(IloAbstractRunShell.java:1614) -> at org.eclipse.core.internal.jobs.Worker.run(Worker.java:53) ->.   callJar.mod    /<myFolder>    2:1-5 <directoryToPath>\callJar.mod    OPL Problem Marker


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 12.  Re: IloOplJavaCall Inside CPLEX IDE

    Posted 04/08/16 11:16 AM

    Hi

    "<pathToDirectory>\callJar.mod"

    looks strange.

    Can you try with a real absolute path like:

    "C:\toto\titi"

    ?

    regards


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 13.  Re: IloOplJavaCall Inside CPLEX IDE

    Posted 04/08/16 11:37 AM

    Originally posted by: mathygirl


    I do have something like "C:\toto\titi", I was just trying to make it generic to not write my actual path.


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 14.  Re: IloOplJavaCall Inside CPLEX IDE

    Posted 04/08/16 11:23 AM

    Originally posted by: FredericDelhoume


    It looks to me that it is not related to having pathes in the arguments, but instead to arguments not passed to the Java call.

    Are you sure myMethod is called ? If yes with what arguments ?

     


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 15.  Re: IloOplJavaCall Inside CPLEX IDE

    Posted 04/08/16 11:24 AM

    Originally posted by: FredericDelhoume


    In fact I am not sure the .jar is loaded at all, so maybe you need to

     IloOplImportJava("<pathtojar>\myJar.jar");


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 16.  Re: IloOplJavaCall Inside CPLEX IDE

    Posted 04/08/16 11:41 AM

    Originally posted by: mathygirl


    myMethod takes as input the directory string "C:\toto\titi". Yes, I do load the jar. Here is the content of the main script. It runs fine when I invoke oplrun from the command line:

     

    main 

     IloOplImportJava("myJar.jar");    
     IloOplCallJava("myPackage.myClass", "myMethod", "", "C:\\toto\\titi"); 
    }


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 17.  Re: IloOplJavaCall Inside CPLEX IDE

    Posted 04/08/16 11:56 AM

    Originally posted by: FredericDelhoume


    On my machine the following works both in IDE and in oplrun :

     

    string input = "C:\\dataDirectoryName";


    main {
     IloOplImportJava("myJar.jar");    
     IloOplCallJava("myPackage.myClass", "myMethod", "(Ljava/lang/String;)V", thisOplModel.input);
    }

     

    with the .java file :

    package myPackage;

    import javax.swing.JFrame;
    import javax.swing.JOptionPane;

    public class myClass {
        public static void myMethod(String path) {
            JOptionPane.showMessageDialog(new JFrame(), path, "Dialog",
                    JOptionPane.ERROR_MESSAGE);
        }

        public static void main(String argv[]) {
            myClass.myMethod("toto");
        }

    }


    Are you sure your Java method is static ?

    If not then it cannot be called because there is no instance of the class.

     

     


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 18.  Re: IloOplJavaCall Inside CPLEX IDE

    Posted 04/08/16 03:37 PM

    Originally posted by: mathygirl


    I am able to run your example, but my own code is still not working in the OPL IDE.

    My code also writes files.

    I've added file-writing to your myClass.java.

    Could you run the code below in the IDE and see if the file pathWrite.txt is generated? It is not being generated for me in the OPL IDE. In Eclipse, yes, but not in the OPL IDE:

    package myPackage;

    import javax.swing.JFrame;
    import javax.swing.JOptionPane;
    import java.io.*;

    public class myClass {
        
        
        public static void myMethod(String path) {
            JOptionPane.showMessageDialog(new JFrame(), path, "Dialog", JOptionPane.ERROR_MESSAGE);
            
            try {
                FileWriter myFile = new FileWriter("pathWrite.txt");
                myFile.append(path);
                myFile.close();
            } catch (IOException e) {
                    e.printStackTrace();
            }        
            
        }

        public static void main(String argv[]) {
            myClass.myMethod("toto");
        }

    }


    #DecisionOptimization
    #OPLusingCPLEXOptimizer


  • 19.  Re: IloOplJavaCall Inside CPLEX IDE

    Posted 04/11/16 05:10 AM

    Originally posted by: FredericDelhoume


    Using this :

     

        public static void myMethod(String path) {
            JOptionPane.showMessageDialog(new JFrame(), path, "Dialog",
                    JOptionPane.ERROR_MESSAGE);

            try {
                FileWriter myFile = new FileWriter("c:\\pathWrite2.txt");
                myFile.append(path);
                myFile.close();
            } catch (IOException e) {
                // e.printStackTrace();
                JOptionPane
                        .showMessageDialog(new JFrame(), e.getLocalizedMessage(),
                                "Error", JOptionPane.ERROR_MESSAGE);
            }

        }

     

     

    The file c:\\pathWrite2.txt is generated running in the OPL IDE and its contents is what I put in the input variable in the OPL model :

    string input = "content";


    main {
     IloOplImportJava("myJar.jar");    
     IloOplCallJava("myPackage.myClass", "myMethod", "(Ljava/lang/String;)V", thisOplModel.input);

    }

     

    The exception is not thrown.

     

    However if the path is not absolute the file is generated, but might not be where you expect it on the disk, I guess it might be somewhere

    near the Java install location, or the OPL installation location.

     


    #DecisionOptimization
    #OPLusingCPLEXOptimizer