Once you have created the file, you want to compile it with javac phptest.java at the command line. This will depend on having the java/bin directory in your PATH environment variable. Once compiled, you can test the class on the command line. Try java phptest, or java phptest hello world. This class is made accessible on the command line, via the main() method. This isn't required for our Java + PHP explorations, but is a nice way to see the class at work.
To test this new class with PHP, create a new php file on your web server called phptest.php. It should contain the following:
<?php $myj = new Java("phptest"); echo "Test Results are <b>" . $myj->test("Hello World") . "</b>"; $myj->foo = "A String Value"; echo "You have set foo to <b>" . $myj->foo . "</b><br>/n"; echo "My java method reports: <b>" . $myj->whatisfoo() . "</b><br>/n"; ?>
If you get a Warning: java.lang.ClassNotFoundException error, it simply means your phptest.class file is not in your java.class.path specified in your php.ini file.
One thing to remember is that Java is strongly typed, and PHP isn't. This could cause problems when Java expects a String, but receives an Integer (or visa-versa). Try replacing the line: $myj->foo = "A String Value"; with $myj->foo = 12345678; and see what happens.
You should cast your variables to the correct type before passing them to Java. eg: $myj->foo = (string) 12345678; or $myj->foo = "12345678";
The phptest.java example is a very simple example of how you can create you own Java class and have PHP access to it.