Page 1 of 1
Java in PHP
Posted: Thu Apr 20, 2006 10:50 pm
by ashishga
Hi All,
I have to call Java code through my PHP script.
I read about PHP/Java Bridge, and have configured my Apache for that.
I want to use java code created by me, that creates a Report through JasperReports, after interacting with the database.
I have included the jar in the PHP code through java_require() method.
But when I pass parameters to the java code through the PHP code, it is returning an error.
Here is my PHP script
Code: Select all
<?php
include ("path_config.php");
if(isset($_POST['email_id'])){
$email = $_POST['email_id'];
}
java_require($host_path . "ReportGenerator.jar;" . $host_path . "jasperreports-1.2.1.jar");
$genrep = new Java("GenReport");
$genrep->genReport($email);
header("Location: ./Sample.htm");
?>
[/quote]
Posted: Fri Apr 21, 2006 12:46 am
by John Cartwright
what is the error?
Posted: Fri Apr 21, 2006 12:55 am
by ashishga
The error is
Fatal error: Uncaught [o(Exception):"java.lang.Exception: Invoke failed: [c(GenReport)]->genReport(o(Request$PhpParserString)). Cause: java.lang.NoSuchMethodException: genReport(o(Request$PhpParserString)). Candidates: []"]
Please help me...
For now, I am sending this parameter in the constructor while instantiating the class, but the class is not performing what it is intended to do, though also not giving error any more.
Regards,
Ashish
Posted: Fri Apr 21, 2006 1:41 am
by ashishga
There is one more thing that I want to know about..
In the code posted by me in my question, I am writing this line
Code: Select all
java_require($host_path . "ReportGenerator.jar;" . $host_path . "jasperreports-1.2.1.jar");
Now I have set the classpath on the server to the JasperReports' project's .jar
Now I want to ask is DO I really have to java_require this jar in my PHP script,
because If I remove this jar from the java_require statement, it gives me this error
Code: Select all
Fatal error: Uncaught [o(Exception):"java.lang.Exception: CreateInstance failed: new GenReport(o(Request$PhpParserString)). Cause: java.lang.ClassNotFoundException: Unresolved external reference: java.lang.NoClassDefFoundError: net/sf/jasperreports/engine/JRException. -- Unable to call constructor because it or one of its parameters refer to the mentioned external class which is not available in the current "java_require()" path. Please check the path and the SEL and File permissions and remember that all interconnected classes must be loaded with a single java_require() call, i.e. use java_require("foo.jar;bar.jar") instead of java_require("foo.jar"); java_require("bar.jar"). Please check the Java Bridge log file for details."] thrown in C:\Program Files\Apache Group\Apache2\htdocs\Testing\Ashish\ITOnline_admin\callRepGen.php on line 7
Please help...
Regards,
Ashish
Posted: Sat Apr 22, 2006 11:44 am
by tasteslikepurple
these are java errors, not php errors. it looks like you've tried to call a function called genReport which doesn't exist.
and the bit about uncaught exceptions means that you need to use try and catch statements. try asking in a java forum about these errors and you might get a little more help

Posted: Mon Apr 24, 2006 1:26 am
by ashishga
Hi All,
I obtained a sample code from the Internet that creates the JasperReports through PHP, but that too is giving a lot of errors.
Please hep me out of this situation :-
My xml file is:
Code: Select all
<?xml version="1.0"?>
<!DOCTYPE jasperReport
PUBLIC "-//JasperReports//DTD Report Design//EN"
"http://jasperreports.sourceforge.net/dtds/jasperreport.dtd">
<jasperReport name="Simple_Report">
<detail>
<band height="20">
<staticText>
<reportElement x="180" y="0" width="200" height="20"/>
<text><![CDATA[Hello World!]]></text>
</staticText>
</band>
</detail>
</jasperReport>
And my PHP script is:-
Code: Select all
<?php
$path = 'http://localhost/Testing/Ashish/';
echo "hi <BR>";
$sJcm = new JavaClass("net.sf.jasperreports.engine.JasperCompileManager");
echo "hi <BR>";
//$report = $sJcm->compileReport($path."Sample.xml");
try{
$report = $sJcm->compileReport("testing.xml");
echo "hi <BR>";
$sJfm = new JavaClass("net.sf.jasperreports.engine.JasperFillManager");
echo "hi <BR>";
$print = $sJfm->fillReport($report, new Java("java.util.HashMap"), new Java("net.sf.jasperreports.engine.JREmptyDataSource"));
$sJem = new JavaClass("net.sf.jasperreports.engine.JasperExportManager");
$sJem->exportReportToPdfFile($print, $path."simple_report.pdf");
}catch(JavaException $ex){
echo "error <BR>";
$trace = new Java("java.io.ByteArrayOutputStream");
$ex->printStackTrace(new Java("java.io.PrintStream", $trace));
print "java stack trace: $trace\n";
}
?>
And the error that I am getting is
Code: Select all
java stack trace: java.lang.Exception: Invoke failed: [c(JasperCompileManager)]->compileReport((String)o(String)). Cause: net.sf.jasperreports.engine.JRException: http:\localhost\Testing\Ashish\testing.jrxml (The filename, directory name, or volume label syntax is incorrect) at net.sf.jasperreports.engine.xml.JRXmlLoader.load(JRXmlLoader.java:171) at net.sf.jasperreports.engine.xml.JRXmlLoader.load(JRXmlLoader.java:151) at net.sf.jasperreports.engine.JasperCompileManager.compileReport(JasperCompileManager.java:142) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:585) at php.java.bridge.JavaBridge.Invoke(JavaBridge.java:1019) at php.java.bridge.Request.handleRequest(Request.java:467) at php.java.bridge.Request.handleRequests(Request.java:493) at php.java.bridge.JavaBridge.run(JavaBridge.java:209) at php.java.bridge.ThreadPool$Delegate.run(ThreadPool.java:29) Caused by: net.sf.jasperreports.engine.JRException: http:\localhost\Testing\Ashish\testing.jrxml (The filename, directory name, or volume label syntax is incorrect) ... 12 more Caused by: java.io.FileNotFoundException: http:\localhost\Testing\Ashish\testing.jrxml (The filename, directory name, or volume label syntax is incorrect) at java.io.FileInputStream.open(Native Method) at java.io.FileInputStream.(FileInputStream.java:106) at net.sf.jasperreports.engine.xml.JRXmlLoader.load(JRXmlLoader.java:166) ... 11 more
And my directory structure is totally perfect, i.e. my xml file is residing at the right position.
Please help me out.
Regards,
Ashish
Posted: Mon Apr 24, 2006 6:05 am
by timvw
"http:\localhost\Testing\Ashish\testing.jrxml" isn't a well-formed URL.
http://localhost/Testing/Ashish/testing.jrxml on the other hand...
Posted: Mon Apr 24, 2006 6:13 am
by ashishga
Exactly...
I don't know why this is taking only one slash between
http and localhost
http:/localhost/Testing/Ashish/testing.jrxml
though in the code, two slashes are provided as in any http request.
Regards,
Ashish
Posted: Mon Apr 24, 2006 8:38 am
by feyd
two backslashes results in a single backslash because it is an escapement character, but as tim says backslash is not used in a well-formed URL; only forward slashes are.
Posted: Mon Apr 24, 2006 10:13 pm
by ashishga
I agree with you...
But if you go through the code that I have provided in the post, there I have written two forward slashes and not used backslashes.