การใช้งาน function exec ของ PHP

ตอบกระทู้

รูปแสดงอารมณ์
:icon_plusone: :like: :plusone: :gfb: :-D :) :( :-o 8O :? 8) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: :wink: :!: :?: :idea: :arrow: :| :mrgreen: :angry: :baa: :biggrin:
รูปแสดงอารมณ์อื่นๆ

BBCode เปิด
[img] เปิด
[url] เปิด
[Smile icon] เปิด

กระทู้แนะนำ
   

มุมมองที่ขยายได้ กระทู้แนะนำ: การใช้งาน function exec ของ PHP

โดย mindphp » 27/02/2007 11:16 pm

ปัญหา คิดว่า น่าจะมาจาก config ค่า ไม่ ถูกน่ะครับ ลอง แก้ ไขค่า config ดู ตามตัวอย่าง ข้างบน นะครับ

โดย Muzashi » 27/02/2007 8:33 am

<?php

$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";

?>

ผมเซฟโค้ดตัวอย่างข้างบนเป็น .php แล้วไปใส่ใน C:\Inetpub\wwwroot พอรันแล้วก็ไม่ขึ้นอะไรเลย ผมก็เลยเพิ่ม echo "test"; ดู มันก็ขึ้นว่า test อย่างเดียว พอ refresh ก็ขึ้นเป็น PHP has encountered an Access Violation at 6D9EEB19 สลับไปมาครับ ไม่รู้เกิดอะไรขึ้น เมื่อวานยังไม่ขึ้นอย่างนี้เลย

โดย mindphp » 26/02/2007 9:03 pm

มาถูกทาง แล้ว นี่ครับ
java.class.path="C:\Program Files\PHP\ext\php_java.jar;C:\Inetpub\wwwroot"
ก็เป็น ที่เก็บ ไฟล์ ของ java ที่ คอมไพล์ แล้ว นะครับ
ในตัวอย่าง เค้า กำหนดไว้ สอง ที่ ที่หนึ่ง คิดว่า น่ะ จะเป็นไฟล์ที่ จำเป็น คือ php_java.jar
ส่วน ไฟล์ java เราที่ คอมไพล์ เป็น .class แล้ว ตามตัวอย่าง จะต้องเก็บ ไว้ที่
C:\Inetpub\wwwroot" สามรถแก้ได้ ตามต้องการ

โดย Muzashi » 26/02/2007 5:13 pm

ช่วยดูให้หน่อยน่ะคับ ด่วนมากๆ ส่วน example code สรุปว่ารันแล้วไม่ขึ้น
ไม่รู้ว่าเซ็ตอะไรผิด หรือว่าต้องเซ็ตอะไรเพิ่มรึป่าว

โดย mindphp » 26/02/2007 4:27 pm

ถ้าผมจำไม่ผิด คิดว่า c:\myclasses เป็น Folder ที่ ใช้เก็บ ไฟล์ java ที่คอมไฟล์ เป็น .class น่ะครับ

โดย Muzashi » 26/02/2007 3:49 pm

ผมลง PHP5.2.1 แบบ msi แล้วก็ใช้กับ IIS แล้วผมก็ไปดาวน์โหลดตัว pecl-5.2.1-Win32.zip

พอลงเสร็จมันจะไปสร้างโฟรเดอร์ที่ C:\Program Files\PHP แต่ว่าในโฟรเดอร์นี้จะไม่มีโฟรเดอร์ ext ผมก็เลย upzip pecl-5.2.1-Win32.zip แล้วก็ไปสร้างโฟรเดอร์ ext แล้วcopy ที่ upzip ไว้ไปใส่

ส่วนใน php.ini ก็จะอยู่ที่ C:\Program Files\PHP ผมก็แก้โดยใส่

extension_dir = "C:\Program Files\PHP\ext"
แล้วก็
[java]
extension=php_java.dll
java.library.path=C:\Program Files\PHP\ext
java.class.path="C:\Program Files\PHP\ext\php_java.jar;C:\Inetpub\wwwroot"
ที่ท้ายไฟล์
แต่ผมไม่รู้ว่า java.class.path="c:\web\php4\extensions\jdk1.2.2\php_java.jar;c:\myclasses" ไอ c:\myclasses คืออะไร ผมคิดว่าคงเป็นที่เก็บไฟล์บนwebserver ก้อเลยใส่ C:\Inetpub\wwwroot ลงไปแทน ไม่รู้ว่าถูกรึป่าวช่วยดูให้หน่อยน่ะคับ

ผมลองรัน example โค้ดแล้วแต่ไม่ขึ้นอะไรเลย เด่วจะลองรีสตาร์เครื่องดูก่อน

โดย mindphp » 26/02/2007 3:06 pm

ลองดูตามรูปนี้ประกอบ นะครับ
[img]http://dev2dev.bea.com/images/2007/02/WithApache.jpg[/img]

ส่วนนี่เป็น วิธี แต่ ค่อนข้างเก่า นะ ต้งอเอามาประยุกนิดหน่อย
PHP & Java
Mark Nold
Joost Soeterbroek
The Java extension is an extremely exciting tool. By learning how to use this module, you can extend PHP by the power of all available Java classes. To show you the basics of the Java extension, this article will cover installation and a few code examples of using PHP and Java together.
Windows Installation
The following configuration has been tested with Apache 1.3.12, PHP 4.0.3 binaries from www.php4win.de plus the 4.0.3 Zend Optimiser and JDK 1.2.2 from java.sun.com. We have also tested this configuration with older versions of the JDK and the various MS webservers (PWS and IIS) on Windows 95, Windows 98 and NT4.
Step 1: Install the JDK. This is fairly simple, as the JDK installs without many questions. It might be useful to check your environment (autoexec.bat in Windows 9x and System under Control Panel in NT) and make sure the jdk1.x.x\bin directory is in your path.
This will make compiling your Java Classes easier. On Win9x add
PATH=%PATH%;C:\jdk1.2.2\bin
to your autoexec.bat. On NT add
;C:\jdk1.2.2\bin
to the end of the PATH environment variable. It is also important to note in your autoexec.bat file, the PHP Java extension ignores the JAVA_HOME and CLASSPATH set up in the environment. This is important because these items must be set correctly in your php.ini file for the Java extension to work.
Step 2: Modifying your php.ini. You need to add something similiar to your php.ini file.

[java]
extension=php_java.dll
java.library.path=c:\web\php4\extensions\
java.class.path="c:\web\php4\extensions\jdk1.2.2\php_java.jar;c:\myclasses"

Typically, people put the extension=php_java.dll directive with the rest of the extensions, but it can sit happily under [java]. The java.library.path must be set in the location of the php_java.dll, and java.class.path must include the location of php_java.jar. The java.class.path should also include the path to other classes you may wish to use (note the double quotes!). In these examples, we will be talking about c:\myclasses. You should also note that the single period is ignored by PHP and Java. As far as we know, you cannot set PHP to look in the current directory for its Java classes.
Step 3: Testing your Install. Now, you're ready to go. Create a PHP file that looks something like this:

<?php

$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";

?>

NB: This is taken directly from Sam Ruby's examples. If you have correctly installed everything, you should see some results like:

Java version=1.2.2
Java vendor=Sun Microsystems Inc.
OS=Windows 95 4.10 on x86
Wednesday, October 18, 2000 at 10:22:45 AM China Standard Time

A very simple example, but it shows you can access currently available Java classes. Once you have this example working, you have successfully set up the PHP Java extension.

โดย mindphp » 26/02/2007 2:47 pm

วิธี แก้ไข ไฟล์ php.ini ไฟล์ นี้ ขึ้นอยู่กับ การ ติกตั้ง ว่าเรา ติดตั้ง php แบบไหน
หาไฟล์ php.ini แล้ว เปิด กับ notepad หรือ โปรแกรมอะไร ก็ได้
แก้
;extension=php_java.dll
เป็น
extension=php_java.dll

ผม ก็ ไม่ได้ เล่น php กับ java นาน แล้ว นะครับ
ตอนนั้นที่ ทำ ทำกับ php4 แต่เดี๋ยว ว่างๆ จะลอง เล่น กับ php 5 ดู ครับ
โดย หลังการ แล้ว เราไม่ต้องแปลง โค้ด แต่ อย่างใด เรา เขียน java ก็ เขียน ไป ตามปกติ ตัว engin ของ จะจัดการ ผ่าน ทาง pecl ให้เอง เดี๋ยว ลอง ดูด้วยกัน ก็ได้ครับ ได้ผล ยังไง มาดู กัน

โดย Muzashi » 26/02/2007 1:57 pm

ผมไม่เข้าใจวิธีการลง PECL คับ ผมใช้ PHP5.2.1 แบบ msi กับ IIS ครับ แล้วผมดาวน์โหลด pecl-5.2.1-Win32.zip มาจะลงยังไงคับ แล้วก็ต้องไป config php.ini ยังไง แล้วก็ช่วยยกตัวอย่างง่ายๆ ในการแปลงโค้ดข้างล่างให้เป็น php ด้วยน่ะคับ ผมอ่านที่ให้มาแล้วไม่เข้าใจน่ะคับ

import java.io.*;

class Hello
{
public static void main(String[] args)
{
System.out.println("Hello World!");
}
}

โดย mindphp » 26/02/2007 11:47 am

ได้อ่าน วิธี ข้างบน และ ลอง ทำหรือ ยังครับ ติดขัดตรงไหน มาคุยต่อกันได้ ครับ
ลองทำ ตามดูก่อน นะครับ

ข้างบน