PHP using Java classes

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
apexclient
Forum Newbie
Posts: 2
Joined: Fri Jul 20, 2007 12:51 pm

PHP using Java classes

Post by apexclient »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


My application is 99% PHP and it uses Java for one small piece.  My custom java class is not found and it seems I’ve tried everything.  Any help will be most appreciated.

The <javapgm>.java compiled cleanly and created <javapgm>.class. 

In the application PHP displays screen, data is entered, then

Code: Select all

<?php
…  PHP code …
$callJava = new Java("javapgm");
… more PHP code
?>
Error message is:

Warning: java.lang.ClassNotFoundException: javapgm

Versions of software:

Apache 2.0.59(Win32)
PHP 4.4.4
Java 1.5.0_01
Windows 2000 5.0

I swapped out my custom Java class for an example I found on the internet:

Code: Select all

$system = new Java("java.lang.System");  
  print "Java version=".$system->getProperty("java.version")." <br>\n";
  print "Java vendor=".$system->getProperty("java.vendor")." <p>\n\n";  
  print "OS=".$system->getProperty("os.name")." ".
              $system->getProperty("os.version")." on ".
              $system->getProperty("os.arch")." <br>\n";

  $formatter = new Java("java.text.SimpleDateFormat","EEEE, 
	MMMM dd, yyyy 'at' h:mm:ss a zzzz");
   print $formatter->format(new Java("java.util.Date"))."\n";
and it properly works and displays:

Java version=1.5.0_01
Java vendor=Sun Microsystems Inc.
OS=Windows 2000 5.0 on x86
Friday, July 20, 2007 at 9:33:06 AM GMT-07:00

I found the documentation regarding the difference between accessing java user classes, extension classes and bootstrap classes, then added my custom class directory (user class) to the php.ini file after java.class.path=:

java.class.path= "c:\php\extensions\php_java.jar; c:\www\slfsrvmar\javaclasses"
java.library.path = "c:\php\extensions; "c:\jdk\jdk1.5.0_01\jre\lib"
java.home = "c:\jdk\jdk1.5.0_01\bin"
java.library = "c:\jdk\jdk1.5.0_01\jre\bin\client\jvm.dll"

I tried swapping the order of the two directories in java.class.path. My custom class did not work and neither did the getPropriety test, above. The second directory in the concatenation does not seem to be accessed. Note, that Apache was stopped/started each time when php.ini was changed and a new browser window was also opened after each Apache cycling.

I then found a java method to change the directory thinking that might be the solution:

Code: Select all

$system = new Java("java.lang.System");  
  print "java.class.path BEFORE=".$system->getProperty("java.class.path")." <br>\n";  
  $system->setProperty("java.class.path", "c:\\www\\slfsrvmar\\javaclasses");
  print "AFTER path change=".$system->getProperty("java.class.path")." <br>\n";
which produced:

java.class.path BEFORE=c:\php\extensions\php_java.jar; c:\www\slfsrvmar\javaclasses AFTER path change=c:\www\slfsrvmar\javaclasses

My class was still not found.

Then modified WINNT environment variable CLASSPATH and added c:\www\slfsrvmar\javaclasses. Rebooted and still the same problem.

I am totally mystified as to what to do next. The PHP/Java interface seems to be working or the Java version and today’s date display would not function.

Any help will be most appreciated. I’m quite new to Java.

Thanks, MAR


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

What does javapgm do?
apexclient
Forum Newbie
Posts: 2
Joined: Fri Jul 20, 2007 12:51 pm

Post by apexclient »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Program [b]javapgm[/b] sets up variables and calls [b]javatoCOBOL2[/b], which  calls [b]javatoCOBOL3[/b].   [b]javatoCOBOL2[/b] calls [b]javatoCOBOL3 [/b] to format variables, padding character variables to the correct length.  [b]javatoCOBOL2 [/b]uses those variables to call a COBOL .dll using [b]cobcall[/b].  See below.

[b]javapgm[/b]:

[syntax="java"]import java.io.Serializable;
 
public class javatoCOBOL1 implements Serializable  

{
  private String assn;
  private String client;
  private String certfill;
  private String ratesw;
  private String plan;
  private String usrnm;
  private String pswd;
  public  String messagetext;  

  public javatoCOBOL1() 
  { 
    assn   = "<?php echo $assn ?>";
    client = "<?php echo $client ?>";
    certf  = "<?php echo $certfill ?>";
    ratesw = "<?php echo $switch ?>"; 
    plan   = "<?php echo $plan ?>"; 
    usrnm  = "<?php echo $usrnm ?>"; 
    pswd   = "<?php echo $pswd ?>";
    messagetext = null;
    searchvalue = assn + client + certf + plan + usrnm + pswd;
  }
}

public String getRateSwitch()
{
   return ratesw;
}
public void setRateSwitch( String switch3 )
{
	 ratesw = switch3;
	 this.executeCobol();
}

public void setSearchvalue( String tmpsearchvalue )
{
   searchvalue = tmpsearchvalue;
   this.executeCobol()
}
private void executeCobol()
{
	  if (this.searchvalue == null)return;
	  
    javatoCOBOL2   rd;
    javatoCOBOL3   calcRate;
    try 
    {
      rd = new javatoCOBOL2();
      calcRate = new javatoCOBOL3( "", "", "", "", "", "", "");
      // setdetails method that calls the cobol
      rd.setDetails(calcRate);
      // put the results back into the fields on the form.
      
      switch      = calcRate.getJswitch();
      messagetext = calcRate.getMessageText();      
    }
    catch (mfcobol.COBOLException e)
    {
      messagetext="javatoCOBOL1 encountered a COBOL exception - " + e;
    }
    catch (Exception e)
    {
      messagetext="javatoCOBOL1 encountered a JAVA exception - " + e;
    }
  } 

[b]javatoCOBOL2:[/b]

import mfcobol.*;

public class javatoCOBOL2
{
  public javatoCOBOL2()
  { 
  }
  public void setDetails(javatoCOBOL3 calcRate) throws COBOLException, Exception
  {
    int retcode;
    // pass the details to the COBOL program
    Object[] params = { calcRate };
    try 
    {
      retcode = runtime.cobcall("ENR625JV", params);
    }
      catch (Exception e)
    {
      e.printStackTrace();
      // System.exit(0);
     }
  }
}

javatoCOBOL3:

Code: Select all

import mfcobol.lang.*;
import java.text.*;

public class javatoCOBOL3 implements DataType
{
    private String inAssn, inClient, inCert, inPlan, inSwitch, inUsrnm, inPswd;
    private String messageText;
    private byte[] bytes;
    
    javatoCOBOL3 (String assn3, String client3, String cert3, String plan3, String switch3, 
                  String usrnm3, String pswd3)
    {
        assn3   = assn;
        client3 = client;
        cert3   = cert;
        switch3 = switch;
        plan3   = plan;
        usrnm3  = usrnm;
        pswd3   = pswd;
        messageText = tmpmessagetext;
    }

    public void synchronizeData()
    {
	// This method is called from COBOL prior to return from a method.
	//
        // We use it here to update each field to contain the
        // data returned by COBOL in the updated byte array.

        inAssn   = new String(bytes, 0, 4);
        inClient = new String(bytes, 4, 6);
        inCert   = new String(bytes, 10, 10);
        inPlan   = new String(bytes, 20, 10);
        inSwitch = new String(bytes, 30, 1);
        inUsrnm  = new String(bytes, 31, 32);
        inPswd   = new String(bytes, 63, 32); 
        messageText = new String(bytes, 95, 64);       
    }

    public byte[] getBytes()
    {
	// This method is called from COBOL on entry to a method to obtain
        // the data to be moved into the COBOL data type.

	// To pass the information, a temporary string is created from the
        // fields in the class, to allow them to be passed to COBOL as a
        // byte array.

        int i;

        StringBuffer cblPassPlan = new StringBuffer(inPlan);
        // The COBOL program is expecting 10 character strings, so space fill.
        for (i = cblPassPlan.length(); i < 10; i++)
             cblPassPlan.append(" ");

        StringBuffer cblPassUsrnm = new StringBuffer(inUsrnm);
        // The COBOL program is expecting 32 character strings, so space fill.
        for (i = cblPassUsrnm.length(); i < 32; i++)
             cblPassUsrnm.append(" ");

        StringBuffer cblPassPswd = new StringBuffer(inPswd);
        // The COBOL program is expecting 32 character strings, so space fill.
        for (i = cblPassPswd.length(); i < 32; i++)
             cblPassPswd.append(" ");        

        String cblString = new String(Jassn.toString() +
                                      Jclient.toString() +
                                      Jcert.toString() +
                                      Jswitch.toString() +
                                      cblPlan.toString() +
                                      cblUsrnm.toString() +
                                      cblPswd.toString());
	bytes = cblString.getBytes();
 	return bytes;
    }

    public String getSwitch()
    {
      return this.inSwitch();
    }

    public String getMessageText()
    {
      return this.messageText.trim();
    }

    public void showRef()
    {
	// Method used to illustrate that the fields were updated.

        System.out.println("assn value is: " + inAssn);
        System.out.println("client is: " + inClient);
        System.out.println("cert is: " + inCert);
        System.out.println("switch is: " + inSwitch);
        System.out.println("plan is: " + cblPassPlan);
        System.out.println("usrnm is: " + cblPassUsrnm);
        System.out.println("pswd is: " + cblPassPswd);
        System.out.println("message string is: " + messageText);
    }
}

feyd | Please use[/syntax]

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Post Reply