By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,466 Members | 1,309 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,466 IT Pros & Developers. It's quick & easy.

Call Java Method from PHP

P: n/a
Hello,

can i call Java method.That is java code from php,here what i m
looking for i have class called DB in this class all the DB related
things ,like DB connection DB insert,DB read etc Methods are defined.

"All i m looking for just call the DB method from PHP that Read
data from DB".
here i m using phptriad2-2-1 that has support for java
i made changes to "php.ini" file i have added the following lines to
this file
[change]

java.class.path = .\php_java.dll
java.home = C:\Program Files\Java\jdk1.5.0_11
java.library = C:\Program Files\Java\jre1.5.0_11\bin\client\jvm.dll
java.library.path = C:\apache\php\extensions\

[/change]
her i don't have php_java.jar so i musing php_java.dll

and here is the follwing simple code.
Expand|Select|Wrap|Line Numbers
  1.  
  2. <HTML>
  3. <HEAD>
  4. <TITLE>Test for Java</TITLE>
  5. </HEAD>
  6. <BODY>
  7. <?php
  8. // get instance of Java class java.lang.System in PHP
  9. $system = new Java('java.lang.System');   //here is the error
  10. // demonstrate property access
  11. echo 'Java version=' . $system->getProperty('java.version') . '<br /
  12.         
  13.                 >';
  14.  
  15. echo 'Java vendor=' . $system->getProperty('java.vendor') . '<br />';
  16. echo 'OS=' . $system->getProperty('os.name') . ' ' .
  17. $system->getProperty('os.version') . ' on ' .
  18. $system->getProperty('os.arch') . ' <br />';
  19.  
  20. // java.util.Date example
  21. $formatter = new Java('java.text.SimpleDateFormat',
  22. "EEEE, MMMM dd, yyyy 'at' h:mm:ss a zzzz");
  23.  
  24. echo $formatter->format(new Java('java.util.Date'));
  25. ?>
  26. </BODY>
  27. </HTML>
  28.  
  29.  

but it gives the following error in browser
[error]

Fatal error: Cannot instantiate non-existent class: java in c:\apache
\htdocs\web\phpjava.php on line 8

[/error]

some body help.

Thanks
VM
Jun 2 '08 #1
Share this Question
Share on Google+
11 Replies


P: n/a
BigZero schreef:
Hello,

can i call Java method.That is java code from php,here what i m
looking for i have class called DB in this class all the DB related
things ,like DB connection DB insert,DB read etc Methods are defined.

"All i m looking for just call the DB method from PHP that Read
data from DB".
here i m using phptriad2-2-1 that has support for java
i made changes to "php.ini" file i have added the following lines to
this file
[change]

java.class.path = .\php_java.dll
java.home = C:\Program Files\Java\jdk1.5.0_11
java.library = C:\Program Files\Java\jre1.5.0_11\bin\client\jvm.dll
java.library.path = C:\apache\php\extensions\
Hi,

Did you try asking PHP what it thinks about the above settings?
Try a new php script, only containing:
<?php
phpinfo();
?>

Can you find the Javasection? Are the paths allright?
I am not 100% sure if PHP fixes this itself, but when using spaces in
pathnames it is wise to pass them around as a string, like:

java.home = "C:\Program Files\Java\jdk1.5.0_11"

>
[/change]
her i don't have php_java.jar so i musing php_java.dll
And you DID make sure this is said so in php.ini too?

Check what phpinfo() says first.

Regards,
Erwin Moller
>
and here is the follwing simple code.
Expand|Select|Wrap|Line Numbers
  1. <HTML>
  2. <HEAD>
  3. <TITLE>Test for Java</TITLE>
  4. </HEAD>
  5. <BODY>
  6. <?php
  7. // get instance of Java class java.lang.System in PHP
  8. $system = new Java('java.lang.System');   //here is the error
  9. // demonstrate property access
  10. echo 'Java version=' . $system->getProperty('java.version') . '<br /
  11.         
  12.                 >';
  •  
  • echo 'Java vendor=' . $system->getProperty('java.vendor') . '<br />';
  • echo 'OS=' . $system->getProperty('os.name') . ' ' .
  •              $system->getProperty('os.version') . ' on ' .
  •              $system->getProperty('os.arch') . ' <br />';
  • // java.util.Date example
  • $formatter = new Java('java.text.SimpleDateFormat',
  •                       "EEEE, MMMM dd, yyyy 'at' h:mm:ss a zzzz");
  • echo $formatter->format(new Java('java.util.Date'));
  • ?>
  • </BODY>
  • </HTML>
  •  


  • but it gives the following error in browser
    [error]

    Fatal error: Cannot instantiate non-existent class: java in c:\apache
    \htdocs\web\phpjava.php on line 8

    [/error]

    some body help.

    Thanks
    VM
    Jun 2 '08 #2

    P: n/a
    ok thanks on that
    ill check on that



    Thanks
    VM
    Jun 2 '08 #3

    P: n/a
    Hello,
    i checked the phpinfo()
    i think it should give me table for java, well it gives info about
    "ODBC SQL XML" ect
    but not for "java" table

    here is example that came from phpinfo() page in table format
    xml
    XML Support active
    XML Namespace Support active
    EXPAT Version 1.95.2

    odbc
    ODBC Support enabled
    Active Persistent Links 0
    Active Links 0
    ODBC library Win32

    mysql
    MySQL Support enabled
    Active Persistent Links 0
    Active Links 0
    Client API version 3.23.39

    etc but this page don't have the java table what to do.

    Thanks
    VM
    Jun 2 '08 #4

    P: n/a
    On 9 May, 08:25, BigZero <vijaymajagaon...@gmail.comwrote:
    Hello,

    can i call Java method.That is java code from php,here what i m
    looking for i have class called DB in this class all the DB related
    things ,like DB connection DB insert,DB read etc Methods are defined.

    "All i m looking for just call the DB method from PHP that Read
    data from DB".

    here i m using phptriad2-2-1 that has support for java
    i made changes to "php.ini" file i have added the following lines to
    this file
    [change]

    java.class.path = .\php_java.dll
    java.home = C:\Program Files\Java\jdk1.5.0_11
    java.library = C:\Program Files\Java\jre1.5.0_11\bin\client\jvm.dll
    java.library.path = C:\apache\php\extensions\

    [/change]
    I'd have expected that you'd need to enable the java extension:

    extension=php_java.dll

    Also, I'd expect your java.class.path to point to where your jar files
    live.
    Similarly java.library.path should point to DLLs to be used by Java -
    not DLLS to be used by PHP.

    HTH

    C.
    Jun 2 '08 #5

    P: n/a
    Well table came in php info
    java
    Directive Local Value Master Value
    java.class.path
    C:\Program Files\Java\jdk1.5.0_11\jre\lib C:\Program Files\Java
    \jdk1.5.0_11\jre\lib
    java.home
    C:\Program Files\Java\jdk1.5.0_11 C:\Program Files\Java\jdk1.5.0_11
    java.library
    C:\Program Files\Java\jre1.5.0_11\bin\client\jvm.dll C:\Program Files
    \Java\jre1.5.0_11\bin\client\jvm.dll
    java.library.path
    C:\Program Files\Java\jdk1.5.0_11\jre\bin C:\Program Files\Java
    \jdk1.5.0_11\jre\bin

    but the error still there.
    Thanks
    VM
    Jun 2 '08 #6

    P: n/a
    Come on some body plz......



    Thanks
    VM

    Jun 2 '08 #7

    P: n/a
    hello to ever one
    the problem solved

    here are changes i made to php.ini file
    and the above code working

    java.class.path = "C:\apache\php\JAVA\php_java.jar; C:\Program Files
    \Java\jdk1.5.0_11\jre\lib"
    java.home = "C:\Program Files\Java\jdk1.5.0_11; C:\Program Files\Java
    \jdk1.5.0_11\lib"
    java.library = "C:\Program Files\Java\jre1.5.0_11\bin\client\jvm.dll"
    java.library.path = "C:\apache\php\extensions; C:\Program Files\Java
    \jdk1.5.0_11\lib"

    Thanks
    VM
    Jun 2 '08 #8

    P: n/a
    Well above thing working fine with the first code but if write my own
    class in java example the following code get gives two error's as
    follows
    [code java]
    public class hello {
    public static void main(String[] args) {
    //do nothing - this will keep us from getting a compile error
    }
    public String SayHello() {
    Date today = new Date();
    return "Hello Geek here is the java date and time: " + today;
    }

    public int SayNumber(){
    int mynum = 0;
    for(int i=1;i<=5;i++){
    mynum =+ (int)(Math.random()*100);
    }
    return mynum;
    }
    }
    [/code java]
    [code php]
    <?php
    //java_require("C:\\apache\\htdocs\\web\\");
    $obj = new Java("hello");
    // Call the "sayHello()" method
    $output = $obj->SayHello();
    echo $output.' this text in PHP<br />'; // Displays (so this comes
    from the class!)
    // Call the "SayNumber()" method
    $output = $obj->SayNumber();
    // Displays (so this comes from the class!)
    echo $output.' is a lucky number';
    //Because the JVM caches everything we want reset while playing with
    the code. Otherwise the
    // a cached version of the same class file will be used rather than
    the new one
    echo "<br />Resetting back-end to initial state\n";
    // suppress the warning message from the use of reset.
    @java_reset();

    ?>
    [/code php]
    well this code gives two error's
    [error]

    Warning: java.lang.ClassNotFoundException: hello in c:\apache\htdocs
    \web\javaphp.php on line 7

    Fatal error: Call to a member function on a non-object in c:\apache
    \htdocs\web\javaphp.php on line 9
    [/error]

    Can some body plz help to solve this thing,
    Thanks
    VM

    Jun 2 '08 #9

    P: n/a
    BigZero wrote:
    Well above thing working fine with the first code but if write my own
    class in java example the following code get gives two error's as
    follows
    [code java]
    public class hello {
    public static void main(String[] args) {
    //do nothing - this will keep us from getting a compile error
    }
    public String SayHello() {
    Date today = new Date();
    return "Hello Geek here is the java date and time: " + today;
    }

    public int SayNumber(){
    int mynum = 0;
    for(int i=1;i<=5;i++){
    mynum =+ (int)(Math.random()*100);
    }
    return mynum;
    }
    }
    [/code java]
    [code php]
    <?php
    //java_require("C:\\apache\\htdocs\\web\\");
    $obj = new Java("hello");
    // Call the "sayHello()" method
    $output = $obj->SayHello();
    echo $output.' this text in PHP<br />'; // Displays (so this comes
    from the class!)
    // Call the "SayNumber()" method
    $output = $obj->SayNumber();
    // Displays (so this comes from the class!)
    echo $output.' is a lucky number';
    //Because the JVM caches everything we want reset while playing with
    the code. Otherwise the
    // a cached version of the same class file will be used rather than
    the new one
    echo "<br />Resetting back-end to initial state\n";
    // suppress the warning message from the use of reset.
    @java_reset();

    ?>
    [/code php]
    well this code gives two error's
    [error]

    Warning: java.lang.ClassNotFoundException: hello in c:\apache\htdocs
    \web\javaphp.php on line 7

    Fatal error: Call to a member function on a non-object in c:\apache
    \htdocs\web\javaphp.php on line 9
    [/error]

    Can some body plz help to solve this thing,
    Thanks
    VM

    I've never tried the Java integration - but I suspect the java_require()
    statement must point at a .jar or .class file, not a directory.

    --
    ==================
    Remove the "x" from my email address
    Jerry Stuckle
    JDS Computer Training Corp.
    js*******@attglobal.net
    ==================

    Jun 2 '08 #10

    P: n/a
    well i did that to but still error

    Java_require("./hello");

    error :
    Fatal error: Call to undefined function: java_require() in c:\apache
    \htdocs\web\javaphp.php on line 7
    Jun 2 '08 #11

    P: n/a
    BigZero wrote:
    well i did that to but still error

    Java_require("./hello");

    error :
    Fatal error: Call to undefined function: java_require() in c:\apache
    \htdocs\web\javaphp.php on line 7
    Which indicates your java bridge isn't properly installed.

    What did you change? This was not the error you were getting before.

    --
    ==================
    Remove the "x" from my email address
    Jerry Stuckle
    JDS Computer Training Corp.
    js*******@attglobal.net
    ==================

    Jun 2 '08 #12

    This discussion thread is closed

    Replies have been disabled for this discussion.