Configuring the Java Project

Create a Java Project

To create the Java project from which to run your application:

  1. In the COBOL Explorer, click File > New > Other.
  2. Click Java > Java Project.
  3. Click Next.

    This opens the New Java Project dialog box.

  4. In the Project name field, type Calc.
  5. Click Use default JRE.
  6. Click Finish.

    When you are prompted to open the associated perspective, click No (as we have already configured the COBOL perspective to show non-Micro Focus projects).

Update the Project Dependencies

In order to see the JVM COBOL code during development, update the project's dependencies:

  1. In COBOL Explorer, right-click the Calc project.
  2. Click Properties.

    This opens the Properties for Calc dialog box.

  3. Click Java Build Path.
  4. Click the Projects tab.
  5. Select Classpath, and then click Add.

    This opens the Required Project Selection dialog box.

  6. Check the CJVM project.
  7. Click OK.
  8. Click Apply and Close.

Create the New Class Containing the Main Method

You now need to create a new class file to contain the main method:

  1. Click the Calc project, and then click File > New > Other.

    This opens the Select a wizard dialog box.

  2. Select Class and click Next.

    This opens the New Java Class dialog box.

  3. In the Package field, type com.calc
  4. In the Name field, type MainClass.
  5. Click Finish.

    This opens the MainClass.java in an editor.

  6. You can now import the my.pack.Calculator class and access it from this Java project. Copy and paste the following code into the editor, overwriting the existing text, and then click Ctrl+S to save.:
    package com.calc;
    
    import my.pack.Calculator;
    
    public class MainClass {
    
        public static void main(String[] args) {
        	Calculator calc = new Calculator();
        	int result = 0;
        	calc.Calculator(1,2,result);
        	System.out.println(result);
        }
    	
    }

    The method in the Calculator class corresponds to the procedure division in the Calculator.cbl program:

    long my.pack.Calculator.Calculator(int lnk-arg1, int lnk-arg2, int lnk-sum)

    The result variable will always be equal to 0 because it uses the by value option for the lnk-sum argument in the procedure division of the Calculator.cbl program. To get a return value, the variable used for result must be passed by reference.

  7. Modify the procedure division in the Calculator.cbl file to:
           procedure division using by value lnk-arg1, 
                                    by value lnk-arg2,
                                    by reference lnk-sum.

    At this point you get a problem marker in the editor for the java class.

  8. Click the Problems view to see a description of the issue saying The type com.microfocus.cobol.program.Reference cannot be resolved.. To resolve this, you need to add the COBOL JVM Runtime System library.
    1. Right-click the Calc project, and then click Properties > Java Build Path.
    2. Click the Libraries tab.
    3. Select Classpath, and then click Add Library.

      This opens the Add Library dialog box.

    4. Click COBOL JVM Runtime System, and then click Next.

      This displays the path to the runtime to be used.

    5. Click Finish.
    6. Click Apply and Close.

      At this point, a problem marker is displayed in the editor for the java class.

  9. Click the Problem view to see a full description of the issue:
    The method Calculator(int, int, Reference) in the type Calculator is not applicable for the arguments (int, int, int).

    With the next steps you will resolve the issue.

  10. You must specify the ILSMARTLINKAGE directive. This exposes the linkage section and entry points to the JVM code by creating types. In the Calculator.cbl file, on the first line at column 7, type:
    $set ILSMARTLINKAGE "my.pack"
    Note: Each $set must be on a new line.
    • Alternatively, you can add the directive to the project's build configuration:
      1. Right-click the CJVM project, and then click Properties > Micro Focus > Build Configuration.
      2. In the Additional directives field, click and in the Additional directives dialog box, type ILSMARTLINKAGE "my.pack", then click OK.
      3. Click Apply and Close.
      Note: Using this method applies the directive to all programs in the project.

    Using this directive results in a new class being created, called LnkSum, which is created in the my.pack package. You can see this in the COBOL Explorer by expanding the Calculator.cbl file:


    LnkSum

    The Problems view still reports one more issue related to the Calculator method.

  11. The calculator's method now requires the following signature:
    (int lnk-arg1, int lnk-arg2, LnkSum my.pack.LnkSum)

    Modify the main method in the MainClass.java file:

    	public static void main(String[] args) {
    		Calculator calc = new Calculator();
    		LnkSum sum = new LnkSum();
    		calc.Calculator(1,2,sum);
    		System.out.println(sum.getLnkSum());
    	}
    Also, ensure that you add the following import:
    import my.pack.LnkSum;

    Your projects now compile cleanly.