I have two java classes that reside in .../jtest/StepRegTest directory and include "package StepRegTest;" directive.
Option 1. I call it directly from shell:
[jtest]# java StepRegTest.Main
Everything works fine.
Option 2. I call java via PHP exec:
<?php
echo getcwd() . "\n";
exec("java StepRegTest.Main 2>&1", $output);
print_r( $output );
?>
The PHP file is in the same directory jtest as for the Option 1, when I call it from the shell. However, it returns an error:
Exception in thread "main" java.lang.NoClassDefFoundError: com/quinncurtis/matpackjava/DMatBase
The jar archive that refers to these missing classes is in the CLASSPATH.
The import reference for that class is:
import com.quinncurtis.matpackjava.DDMat;
Note that everything works fine when I execute the code from the shell.
What is the problem here? Why does it work from the shell, but not from PHP?
Thanks!
If the CLASSPATH environment variable is being used, verify it's set at the time the php exec() call is made; it may be getting lost.
One easy way to check is this:
<?php
echo getcwd() . "\n";
exec("env", $output);
print_r( $output );
?>
To set the CLASSPATH variable in the php script, the following can be done:
putenv("CLASSPATH=/path/to/lib.jar");
Here's a page that describes putenv():
http://php.net/manual/en/function.putenv.php
Related
I tried JAVA bridge to get output from my jar file followed this https://stackoverflow.com/a/10253798/1870048, I am using XAMP Server and from that Apache and Tomcat server is been used
require_once("http://localhost:8080/JavaBridge/java/Java.inc");
$cartObj = Java("Cart"); //Javav("Cart");
$cartObj->Cart("098765");
$cartObj->setLang("EN");
echo $cartObj->getLang();
but gives error :
Fatal error: Call to undefined function Javav()
What should I exactly I look into?
Also I tried below way also to call the jar file by placing the jar file in my zampp htdoc folder: (D:\xampp\htdocs\jar)
// Using shell_exec
exec('java -jar AutoDiscounts.jar '.$jsonRequest.' 2>&1', $result);
print_r($result);
// Using shell_exec
$arg1 = $jsonRequest;//"My_INPUT_PARAMETER";
$output = '';
$output =shell_exec("java -jar AutoDiscounts.jar $arg1");
echo "Done calling shell exec:";
echo $output;
But both above cases does not give any output .Basically my jar will do some cart processing and return output and it works in the JDevloper
I created a .sh file with the following content:
java -jar selenium-server-standalone-3.3.1.jar -Dwebdriver.gecko.driver=./opt/webdrivers/geckodriver
I already made sure that the file /opt/webdrivers/geckodriver has permission to get executed by using chmod 770 and I even changed the user to root.
But this is the result if I execute my .sh file:
root#mycomputer:/opt/Selenium# ./selenium.sh
Exception in thread "main" com.beust.jcommander.ParameterException: Unknown option: -Dwebdriver.gecko.driver=./opt/webdrivers/geckodriver
at com.beust.jcommander.JCommander.parseValues(JCommander.java:742)
at com.beust.jcommander.JCommander.parse(JCommander.java:282)
at com.beust.jcommander.JCommander.parse(JCommander.java:265)
at com.beust.jcommander.JCommander.<init>(JCommander.java:210)
at org.openqa.grid.selenium.GridLauncherV3$1.setConfiguration(GridLauncherV3.java:227)
at org.openqa.grid.selenium.GridLauncherV3.buildLauncher(GridLauncherV3.java:155)
at org.openqa.grid.selenium.GridLauncherV3.main(GridLauncherV3.java:75)
What am I doing wrong?
You have to specify the parameters right after -jar:
java -jar -Dwebdriver.gecko.driver=./opt/webdrivers/geckodriver selenium-server-standalone-3.3.1.jar
Note: This is a knowledge sharing answer (share your knowledge, Q&A-style)
Following Getting started -
(Windows 7, console: C:...>, .jar & .javauto in the same folder)
java -jar javauto.jar => OK, help is displayed
javauto hello.javauto => command not recognised
javauto.jar hello.javauto => NO result
So i tried this
java -jar javauto.jar -v hello.javauto
Giving this exception (NB: c:...>javac => help display is OK)
Generating ←[92mhello←[0m...
Getting user imports... None
Getting user global variables... None
Getting user functions... None
Generating functions... ←[93mmouseMove ←[0m←[93mmsgBox ←[0m←[93mprint ←[0m←[93msleep ←[0m
Generating class variables... ←[93msimulatedMotionSpeed ←[0m
Generating imports... ←[93mAWTException ←[0m←[93mMouseInfo ←[0m←[93mRobot ←[0m←[93mJDialog ←[0m←[93
mJOptionPane ←[0m
Generating struct objects... None
Generation complete... starting build
Executing cmd /c attrib +s +h "C:\Users\Francis\Develop\java\Javauto\.hello"...
Building ←[92mC:\Users\Francis\Develop\java\Javauto\.hello\class\hello.class←[0m...
Exception in thread "main" java.lang.NullPointerException: Couldn't find java system compiler.
at com.automation.javauto.compiler.CustomJavaCompiler.compile(CustomJavaCompiler.java:36)
at com.automation.javauto.parser.Create.main(Create.java:419)
Try this:
1) Compile it using this command:
java -jar javauto.jar hello.javauto
2) Now that you have compiled your program, there will be a file called hello.jar. Execute with:
java -jar hello.jar
i am using jenkins in ubuntu and i need to call a java class from python script. The code:
import os
import shutil
import sys
from subprocess import call, STDOUT
param1=os.getenv(‘PARAM1’)
param2=os.getenv(‘PARAM2’)
param3=os.getenv(‘PARAM3’)
cmd1 =”cp /…/Class.class $JENKINS_HOME/jobs/$JOB_NAME/builds/$BUILD_NUMBER/Class.class ”
cmd2=”java $JENKINS_HOME/jobs/$JOB_NAME/builds/$BUILD_NUMBER/Class ” +””+param1+””+param2””+param3
print>>> sys.stder, “Launching command: “ + cmd2
call(cmd1,shell=True)
call(cmd2,shell=True)
But the console output shows “Error: Could not find or load main class”
I have checked an the file was copied, and Jenkis have installed the Java SE Development Kit 8u31 version.
I have try build the process in two step, first copy the java file and later set up the variables and do the second call but appears the same error.
Thanks,
i have changed the code to:
classpath=os.path.join(os.getenv('JENKINS_HOME'),"jobs",os.getenv(JOB_NAME'),"builds",os.getenv('BUILD_NUMBER'))
cmd2=[“java”,”-classpath”,classpath,”Class”,param1,param2,param3]
call(cmd2)
The code Works!!!
When i build with parameters the console output shows "Usage_ java [- options] class [args...]..."
Java doesn't support "run this file as a class" directly. Instead, you need to add the class to the classpath and then call it using the Java Fully Qualified name:
java -classpath $JENKINS_HOME/jobs/$JOB_NAME/builds/$BUILD_NUMBER com.foo.Class ...
would run the Java code in .../builds/$BUILD_NUMBER/com/foo/Class.class
Note: Avoid call() with a string. Instead build a list of command plus arguments. That way, you can replace variables correctly and spaces in file names won't cause unexpected/hard to find problems:
classpath = os.path.join(os.genenv("JENKINS_HOME), "jobs", ...)
cmd = [
"java",
"-classpath",
classpath,
"Class",
...
]
call(cmd)
I am writing a simple web upload script.
The goal is to upload a file using php, and then calling a java program to process this file.
I have done the work for uploading the file, but I cannot get a java program to be successfully run from within the php script.
I have tried exec(), shell_exec(), and system() with no results.
For the command, I have used "java Test", "java < directory >/Test", "/usr/bin/java < directory >/Test", I have even set up the application as a jar file with no results. The actual line of code I have used is:
echo shell_exec("java Test");
Usually there is no output. However, if I have just shell_exec("java"), then the last line of the help from java ("show splash screen with specified image") is displayed, which shows that the command has been executed. If I use, for example, shell_exec("whoami") I get "nobody" returned, which is correct. The only thing the java file does is create a file so that I can see that the application has been successfully run (the application runs successfully if I run it on the command line). I have set the permissions for the java file to 777 to rule out any possibility of permission errors. I have been struggling with this for a while trying all sorts of options with no results - the file is never created (the file is created with an absolute path so it's not being created and I just can't find the file). Does anyone have any ideas?
Thanks.
I have been struggling with this for a
while trying all sorts of options with
no results - the file is never created
(the file is created with an absolute
path so it's not being created and I
just can't find the file). Does anyone
have any ideas?
What I think the problem is. Apache runs as "nobody" group??(apache user??) which will execute the java script which will try to create a file on disc somewhere. I assume it does not have permission to write to that location. you should chown that folder so that apache user can write to that folder.
==
First off I would like to point out to you that calling exec() from a script could really blow up your server. I would advice you to use something like redis(see below) instead.
==
Second I think I know what the problem is. You should first try to run the simple example below which worked fine for me.
==
First be sure permission are set right. Because apache runs as nobody(most of the times).
I tried this simple test myself on ubuntu with php installed from repo.
test.java
class test {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
test.php
echo exec('java test');
Ran test.php
$ php test.php
Hello World!
==
Or you could try 1 of the following solutions(which would even be a better solution):
Write your java program as a webservice for example on top of atmosphere-spade-server(simple/embedded jar). This could be written insanely fast. But on high load this will not be best option I guess. Still I think this will be more than fast enough for you probably. Even this way it will be much faster as executing it, because you won't have the overhead running JVM. Could blow up your server, not as fast as exec()
Do a blocking pop/push from a redis(*nix) list structure. This will be pretty easy to write on *nux because there are client libraries for both java/php. The speed will best I guess because redis is written in C. I use redis myself.
Use a JMS like for example activemq. Also pretty easy to write because good library support. I have not used a JMS myself. I use redis solution. The speed I guess would be a little less then with redis solution.
I dont realy know, but i came a cross PHP-JAVA bridge maybe it can help
http://php-java-bridge.sourceforge.net/pjb/
Update:
I tested this with Jasper Reports, and it is working really nice. It will allow you to Extend Java classes with PHP or just use Java class lik it was PHP.
use java\lang\String as JString;
require_once("javabridge/java/Java.inc");
class String extends JString {
function toString () {
return "hello " . parent::toString();
}
}
$str = new String("Java");
echo $str->toString();
or
$temp = new Java('java.sql.Timestamp');
$javaObject = $temp->valueOf('2007-12-31 0:0:0');
$params = new Java("java.util.HashMap");
$params->put("text", "This is a test string");
$params->put("date",$javaObject);
More examples: http://php-java-bridge.sourceforge.net/pjb/FAQ.html
It's possible it has to do with the path that the exec is defaulting to. You may need to explicitly define your classpath with an absolute path to your .class or jar files when calling java.
<?php
$PATH="C:\Program Files\Java\jdk1.7.0_09\bin";
echo exec("javac theNameOfYourJavaProgram.java 2>&1");//shows # of errors
echo "<br />";
echo exec("java theNameOfYourJavaProgram 2>&1");//this line executes it
echo "<br />";
echo shell_exec("javac theNameOfYourJavaProgram.java 2>&1 ");//compiles it
?>