How to call custom java methods from PHP? - php

I am new to both PHP and Java. Currently I am working on a project which call java class and its methods in the PHP code. I am creating a proof of concept for this. The problem is I am not able to connect custom class I have created in java. More over it could be said I don't know how to do it. I have used java.inc and javabridge.jar files to connect to the system classes but not able to connect to a simple class.
The step I have followed is:
Created a java package, class called clsForPHP and it has a method sum() which takes 2 parameters and returns integer value. (This is created using MyEclipse IDE)
Now I am trying to call this function from PHP. I have copied the jar file containg the package to PHP project. (Eclipse- Helios is the IDE)
$d= new java("clsForPHP.class");
Please help!! I have searched a lot but couldn't able to find a proper solution. What I think is this package should be added in the java.inc file but I don't know how to do it.

First of all install Tomcat6 and Java in you Unix box. Most probably your Tomcat will be on 8080 port. Now Download JavaBridge.war from http://php-java-bridge.sourceforge.net/pjb/ unzip it. Then from WEB-INF/lib folder copy JavaBridge.jar ,php-servlet.jar and php-script.jar in lib folder of Tomcat. Then copy JavaBridge.war in your Tomcat6 webapps folder and restart your tomcat6 service which will deploy automatically JavaBridge folder in your webapps
Now try to browse http://localhost:8080/JavaBridge/. If you get working than your PHP/Java Bridge installed. First Part is over.
Now make test page in your apache as below
<?php
require_once("http://localhost:8080/JavaBridge/java/Java.inc");
echo java("java.lang.System")->getProperties();
?>
and run it. If it works then you can start working with your work. If it not work then you have problem with you php.ini file. Try to make allow_url_once=on in your php.ini.
Now just copy your java jar file in tomcat /webapps/JavaBridge/WEB-INF/lib/ folder
and Just always put following line in your page where ever you want java to work
require_once("http://localhost:8080/JavaBridge/java/Java.inc");
$yourObj = Javav("your java class");
$yourObj->yourMethod();
$yourObj->setProperty("xxx");
Hope this can help you out.
IF you have still any problem get back.
Yea it will throw error because you must not have copied your java compiled file may be your jar file in PHP / Java Bridge.
You can to do in following 2 options way
If you have define your CATALINA_HOME then copy
$CATALINA_HOME/webapps/JavaBridge/WEB-INF/lib
If you have not define your CATALINA_HOME then copy
/var/lib/tomcat6/webapps/webapps/JavaBridge/WEB-INF/lib
Path which I am telling it assumes that you have install tomcat6, may be your tomcat webapps folder may be in different path.

Related

Reading file and directory owner using PHP on the Windows

I have an intranet project based on the PHP and xampp server on the Windows. That intranet project have database with table where I store scanned disc drive informations of files and folders. I using simple windows command dir "$directory_path" /Q /T:C to get informations.
After that I use regex to match and parse that info to separate date, time, permission and dir name like on this example:
EXAMPLE REGEX FOR DIRECTORY PARSING
It's similar like this example: Get file owner in Windows using PHP
This working fine but sometimes file owner names are realy long and was stripped by command line. In that case I can't read full name and functionality is broken.
Now I searching for another solution.
My PHP have instaled also php_com_dotnet.dll extension and I using COM() class in some part of code.
My main question is:
-Can I use COM() class to get real file informations and avoid shell commands for the file search and listings and how?
-Is there some another extension, shell command or something 3rd what I can use to get file/folder owners?
NOTE: I need this only for the reading and indexing in the database, don't need to change permissions or ownership.
I find simple solution for this using PHP class COM() and the documentation of the IADsSecurityUtility::GetSecurityDescriptor method.
The code in PHP looks like this:
$path = 'D:\Some File\Some Another File\document.doc'; // File or dir path
$su = new COM("ADsSecurityUtility"); // Call interface
$securityInfo = $su->GetSecurityDescriptor($path, 1, 1); // Call method
echo $securityInfo->owner; // Get file owner
That's all folks.
NOTE
COM functions are only available for the Windows version of PHP.
.Net support requires PHP 5 and the .Net runtime.
As of PHP 5.3.15 / 5.4.5, this extension requires php_com_dotnet.dll
to be enabled inside of php.ini in order to use these functions.
Previous versions of PHP enabled these extensions by default.
You are responsible for installing support for the various COM objects

PHPSpreadsheet - How Do I Use Auto Migration Tool

I'm a long time reader, but new to asking questions. Please correct me if I have somehow asked incorrectly.
Intro
We are upgrading our servers from PHP 5.4 to PHP 7.2 and one of the libraries that is not fully compatible with the new version of PHP is PHPExcel. So, we are upgrading to PHPSpreadsheet as well. A lot of our projects use PHPExcel in different ways, so if I can get the Auto Migrate tool to work, it would be a huge time save for me even if it misses some things.
The instructions to use the tool are found here:
https://phpspreadsheet.readthedocs.io/en/develop/topics/migration-from-PHPExcel/
And they simply say to use the following command after installing via Composer (which I've done):
cd /project/to/migrate/src
/project/to/migrate/vendor/phpoffice/phpspreadsheet/bin/migrate-from-phpexcel
Problem
I am able to execute the migrator fine, but it only scans files inside the "vendor/phpoffice/phpspreadsheet" directory. It does not scan any files inside my project at all.
How do I get the migration script to scan my projects files, and not it's own files?
Local Environment
Windows 10
Latest XAMPP
PHP 7.2.11
How I Am Running It
cd C:\path\to\project\that\uses\phpexcel
php vendor\phpoffice\phpspreadsheet\bin\migrate-from-phpexcel
How I have Tried To Solve So Far
Running the exact command on the instructions page (ends up running two commands, second one is an invalid command)
Moving "migrate-from-phpexcel" to my project root and running it there
Editing "migrate-from-phpexcel" after moving to project root and pointing to the Bootstrap.php file (Runs, but only inside the library itself. Same problem)
I don't know how, or from where, you ran the 2nd command but that command traversed my whole project and made what looks like appropriate conversions. I ran it from project root while I was IN my IDE (PhpStorm, using its 'Run Command' tool). I ran the same command you did, like this:
php vendor\phpoffice\phpspreadsheet\bin\migrate-from-phpexcel
After confirming "yes", I saw it traverse the directory, changing various files but skipping its OWN files.
When I tested my script, I encountered a warning about 'a non-numeric value' from one of its modules (Coordinate.php on line 312 & line 313). I did some research on the warning but couldn't find anything to address it except people blaming PHP 7.1** (I'm running 7.2.*). So I suppressed the warning and my spreadsheet was created exactly as I specified.
Here is my first statement in my 'create excel' script:
error_reporting(E_ALL & ~E_NOTICE & ~E_WARNING);
The only other "manual" change I had to make was changing the file format of the 'createWriter' from 'Excel2007' to 'Xlsx' -- exactly as it states in the docs. A more complicated spreadsheet may require more manual work, but probably not as much as someone above indicated.
In case it helps someone else: while updating a legacy project that was using PhpExcel v1.8.2, running rector on the codebase failed on most of the files that needed to be processed, with the error:
[ERROR] Could not process "test.php" file, due to:
"Analyze error: "PHPExcel_Writer_Exception (Unable to load PDF Rendering library) thrown while looking
for class PHPExcel_Writer_PDF_DomPDF.". Include your files in "$parameters->set(Option::AUTOLOAD_PATHS,
[...]);" in "rector.php" config.
See https://github.com/rectorphp/rector#configuration".
To get around this, I followed suggestions here: https://github.com/rectorphp/rector/issues/6418#issuecomment-882923495 and commented out lines in the PhpExcel install itself - the lines containing throw new PHPExcel_Writer_Exception('Unable to load PDF Rendering library'); in files DomPDF.php, mPDF.php, tcPDF.php in Classes/PHPExcel/Writer/PDF/
2022+ Automated Upgrade Approach
Discalimer: I wrote a tool that handles automated migration from CLI called Rector, so people don't have to upgrade PHP code manually anymore. It's free, open-source, on GitHub.
The offical migration tool only fixes class names. There are 24 more cases, that needs to be changed, lLike mentioned "'Excel2007' to 'Xlsx'".
We needed to upgrade huge PHP project and manual changes would take too much time and could stuck us for weeks. Instead, we made an upgrade set with all 25 cases, that changed code for us:
In 4 steps ↓
1. Install Rector to your PHP project
composer require rector/rector-phpoffice --dev
2. Create rector.php config
vendor/bin/rector init
3. Add the spreadsheet set:
use Rector\PHPOffice\Set\PHPOfficeSetList;
use Rector\Config\RectorConfig;
return static function (RectorConfig $rectorConfig) {
$rectorConfig->sets([PHPOfficeSetList::PHPEXCEL_TO_PHPSPREADSHEET]);
};
4. Run Rector on your code
# to see diff with no changes
vendor/bin/rector process src --dry-run
# to make changes happen
vendor/bin/rector process src
If you want to read more, we wrote a short post about it

Configuring php-cs-fixer and PATH variable

A while ago, I successfully configured Atom to beautify my PHP using php-cs-fixer, but now I need to go through the same process on another Mac and can't remember exactly how I did it.
As a front-end dev, I'm not a PHP expert, but I understand that the Atom plugin is merely the middleman facilitating the actual fixing using php-cs-fixer, which, by default, it doesn't have access to. Once I installed the fixer, I had to add its location to my PATH variable. The confusing part is that I don't have the fixer installed via Atom (checked ~/.atom/packages) nor Homebrew (confirmed with brew list), however, the file is present in /usr/local/bin. If I move it, Atom returns the original error, so it's definitely using this file:
See https://github.com/FriendsOfPHP/PHP-CS-Fixer for program installation instructions.
Your program is properly installed if running 'which php-cs-fixer' in your Terminal returns an absolute path to the executable. If this does not work then you have not installed the program correctly and so Atom Beautify will not find the program. Atom Beautify requires that the program be found in your PATH environment variable.
Note that this is not an Atom Beautify issue if beautification does not work and the above command also does not work: this is expected behaviour, since you have not properly installed your program. Please properly setup the program and search through existing Atom Beautify issues before creating a new issue. See https://github.com/Glavin001/atom-beautify/search?q=php-cs-fixer&type=Issues for related Issues and https://github.com/Glavin001/atom-beautify/tree/master/docs for documentation. If you are still unable to resolve this issue on your own then please create a new issue and ask for help.
Hide Stack Trace
Error: Could not find 'php-cs-fixer'. The program may not be installed.
at PHPCSFixer.module.exports.Beautifier.commandNotFoundError (/Users/ourcore/.atom/packages/atom-beautify/src/beautifiers/beautifier.coffee:204:14)
at /Users/ourcore/.atom/packages/atom-beautify/src/beautifiers/beautifier.coffee:304:22
at tryCatcher (/Users/ourcore/.atom/packages/atom-beautify/node_modules/bluebird/js/release/util.js:16:23)
at Promise._settlePromiseFromHandler (/Users/ourcore/.atom/packages/atom-beautify/node_modules/bluebird/js/release/promise.js:510:31)
at Promise._settlePromise (/Users/ourcore/.atom/packages/atom-beautify/node_modules/bluebird/js/release/promise.js:567:18)
at Promise._settlePromise0 (/Users/ourcore/.atom/packages/atom-beautify/node_modules/bluebird/js/release/promise.js:612:10)
at Promise._settlePromises (/Users/ourcore/.atom/packages/atom-beautify/node_modules/bluebird/js/release/promise.js:687:18)
at Async._drainQueue (/Users/ourcore/.atom/packages/atom-beautify/node_modules/bluebird/js/release/async.js:138:16)
at Async._drainQueues (/Users/ourcore/.atom/packages/atom-beautify/node_modules/bluebird/js/release/async.js:148:10)
at Async.drainQueues (/Users/ourcore/.atom/packages/atom-beautify/node_modules/bluebird/js/release/async.js:17:14)
at process._tickCallback (internal/process/next_tick.js:103:7)
which php-cs-fixer returns /usr/local/bin/php-cs-fixer and my PATH variable currently contains /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/ourcore/.atom/packages/php-cs-fixer.
I guess my questions are, how is the fixer working if the PATH variable seems to be pointing to it in a different location, and how did it end up where it is? Does the location in my PATH matter at all, or does the Atom plugin know to look in /usr/local/bin? I just want to fully understand the entire process before repeating it.
EDIT: On further testing, I removed the Atom directory from my PATH and it didn't affect the plugin, so then I copied the file into the same directory onto the second Mac and it ran without issue, without needing to edit the PATH. Does the fixer file someone inform the OS?
Well first test "which php-cs-fixer" or "where php-cs-fixer" (depending on what os you're running) and if nothing is returned in the terminal then there actually is something wrong on the installation and the best thing to do is to manually install it.
Head to https://github.com/FriendsOfPHP/PHP-CS-Fixer and download it from there and follow the installation steps, after that it should all be good

which is the environment variables route for cakephp 3?

I recently installed a cakephp with composer to learn about framework use, and trying the bookmarks tutorial, but I get stucked in the checking your installation where you have to open the command line AKA cmd, and writing bin\cake.
It replies back: the system cannot find the path specified.
I've seen some information about how to set up the path variables but I'm guessing it's for version 2+ because apparently the cakephp 3 doesn't have a LIB folder like such:
YOUR_CAKE_DIR/lib/Cake/Console/
Any ideas on how to fix this?
Thanks
best regards!
***************UPDATE**********************
well, I've finally fixed the problem, the path I used for the cakePHP 3 instalation was:
c:\xampp\htdocs\caketest\bin
so I assume that If you aren't using xampp, it should point at the end to: cakeinstallation\bin...
my problem was that I always typed bin\cake but actually all I had to do was type only:
cake
you can read more information in here
bin\cake shouldn't require any special path settings other than being in the correct directory. However, you should make sure that php-cli is on your system's PATH. Depending on how you install PHP it could be in any number of places, but if php -v doesn't output the version of PHP you installed, you'll need to find where your installer put PHP and then setup the PATH environment variable.
With your example, AFAI remember the working directory they were executing cake binary was the parent of "bin" folder, in your case "caketest". I am talking without trying it (I dont have an environment close) you should use:
c:\xampp\htdocs\caketest> "bin/cake"
Watch the quotes. I think they are the main reason of that "the system cannot find the path specified."
I am supossing you are under windows (I saw xampp written in your post) and probably the xampp installation has already added the php folder in the PATH environment variable.

Zend_Tool setup problems

hai
I read that to create a project, execute the zf Windows windows command (zf.bat):
C:\>zf.bat create project newproject
I saw zf.bat in my C:\wamp\www\Zend\bin, I want to create a new project in c:\wamp\www
When I tried to run c:\wamp\www\zf.bat, I got an error message:
"zf.bat is not internal or external command, operable program or batch file.."
Then I tried another method:
c:\wamp\zend\bin\>zf.bat create project newproject
But I got the following error message:
"php is not recognised internal or external command,oprable program or batch file"
I put the Zend Framework in c:\wamp/Zend and I added C:\wamp\zend\library in to my php.ini.
Please help me troubleshoot this problem.
Thanks!
You need to add php and the zend framework paths to your system environmental variables:
Right click on My Computer and select properties
Go to the advanced tab, and click on Environmental Variables
In the system variables box, scroll till you find the "Path" variable, select it and click the edit button.
Paths are separated using a semicolon ";". Add the path to the zend framework bin folder, and your php bin folder.
Click ok,ok,ok
You should now be able to use the zf command line tool anywhere you need it by typing "zf" to get the help info and to ensure you put the paths in properly.
Hello you probably need to have php.exe on your path to use zf.bat
try to edit the zf.bat and make sure to use the full path in the script your php.exe is probably located in your wamp directory somewhee

Categories