473,387 Members | 3,820 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Call Java Method from PHP

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
11 11950
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
    ok thanks on that
    ill check on that



    Thanks
    VM
    Jun 2 '08 #3
    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
    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
    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
    Come on some body plz......



    Thanks
    VM

    Jun 2 '08 #7
    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
    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
    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
    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
    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 thread has been closed and replies have been disabled. Please start a new discussion.

    Similar topics

    6
    by: Robbie Baldock | last post by:
    Hi - I'm a bit of a newbie to the world of XSLTs but am trying to call a Java method on a parameter passed into an XSLT but am having problems. I've stripped the XSLT down to its bare bones: ...
    2
    by: Neil Cherry | last post by:
    I've done a fair amount of searching on this subject and I can call my Java methods with Javascript if I use the <applet></applet> tags but when I switch over to <object></object> tags I'm not...
    2
    by: mhust6 | last post by:
    I am trying to call a java method from within my Javascript, but cannot seem to get it to work. All the examples I have found online and in the forums are using Java applets. I have a method that...
    6
    by: Christopher | last post by:
    Its been awhile and I am rusty. Can the constructor of my class call another method in the same class if that other method does not change any member data? I want to simply have a seperate...
    1
    by: manjava | last post by:
    hello, i want to test my service web i write this class to call my method appel:import projetd.MagasinServicesStub.ListeMagasinProcheDeResponse; import java.rmi.RemoteException; public class...
    2
    by: swamyvnvs | last post by:
    HI friends, i have a problem when i call a java method from php code, i saw php.net site ifollow all thr steps , but i got the classNotFoundExceptions. my code is like this JavaClass ...
    5
    by: brandy3619 | last post by:
    Please find below my Java script method in which i call a java method to fetch a locale specific error message alert(<%=Msgs.getMessages("MessageResources", "errors.textNotLatin"); I can get the...
    1
    by: makweatan | last post by:
    Hi..I am trying to call the method in XC18Action through the xcAction. The error was: 0000002f SystemErr R at org.apache.commons.beanutils.BeanUtilsBean.copyProperties(BeanUtilsBean.java:221)...
    0
    by: PriyaPavithra | last post by:
    Hi, I have created java web application. I want to call the setName() method in java application. how is call web method in application?. package sv; import javax.jws.WebMethod; import...
    0
    by: taylorcarr | last post by:
    A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
    0
    by: Charles Arthur | last post by:
    How do i turn on java script on a villaon, callus and itel keypad mobile phone
    0
    by: aa123db | last post by:
    Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
    0
    by: ryjfgjl | last post by:
    If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
    0
    by: emmanuelkatto | last post by:
    Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
    0
    BarryA
    by: BarryA | last post by:
    What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
    1
    by: nemocccc | last post by:
    hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
    0
    by: Hystou | last post by:
    There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
    0
    marktang
    by: marktang | last post by:
    ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

    By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

    To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.