1

When I run command from console, like this, it works:

java -cp /root/weka-3-6-11/weka.jar weka.filters.unsupervised.attribute.Remove {Parameters come here}

But when I run it with PHP, it gives me an error, I think it doesn't use the provided classpath:

exec('java -cp /root/weka-3-6-11/weka.jar weka.filters.unsupervised.attribute.Remove {Parameters come here}');


[0] => Exception in thread "main" java.lang.NoClassDefFoundError: weka/filters/unsupervised/attribute/Remove
[1] => Caused by: java.lang.ClassNotFoundException: weka.filters.unsupervised.attribute.Remove
[2] =>  at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
[3] =>  at java.security.AccessController.doPrivileged(Native Method)
[4] =>  at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
[5] =>  at java.lang.ClassLoader.loadClass(ClassLoader.java:323)
[6] =>  at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
[7] =>  at java.lang.ClassLoader.loadClass(ClassLoader.java:268)
[8] => Could not find the main class: weka.filters.unsupervised.attribute.Remove. Program will exit.

Somewhere I have read that PHP uses its own class path for java, but I cannot get it to work. So far I have tried to execute these lines before the exec command, but still the same exception:

echo getcwd() . "\n"; //  /var/www/yugo_api/web
exec("env", $output);
print_r( $output ); // output is below

Array
(
[0] => APACHE_RUN_DIR=/var/run/apache2
[1] => APACHE_PID_FILE=/var/run/apache2/apache2.pid
[2] => PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
[3] => APACHE_LOCK_DIR=/var/lock/apache2
[4] => LANG=C
[5] => APACHE_RUN_USER=www-data
[6] => APACHE_RUN_GROUP=www-data
[7] => APACHE_LOG_DIR=/var/log/apache2
[8] => PWD=/var/www/yugo_api/web
)

putenv("CLASSPATH=".$cp); // value of $cp is /root/weka-3-6-11/weka.jar

Commands run in terminal:

env

TERM=xterm-256color
SHELL=/bin/bash
SSH_CLIENT=XXX
SSH_TTY=/dev/pts/2
USER=root
LS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arj=01;31:*.taz=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lz=01;31:*.xz=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.axv=01;35:*.anx=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=00;36:*.au=00;36:*.flac=00;36:*.mid=00;36:*.midi=00;36:*.mka=00;36:*.mp3=00;36:*.mpc=00;36:*.ogg=00;36:*.ra=00;36:*.wav=00;36:*.axa=00;36:*.oga=00;36:*.spx=00;36:*.xspf=00;36:
MAIL=/var/mail/root
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
PWD=/var/www/yugo_api
LANG=en_US.UTF-8
SHLVL=1
HOME=/root
LANGUAGE=en_US:en
LOGNAME=root
SSH_CONNECTION=XXX
LC_CTYPE=UTF-8
LESSOPEN=| /usr/bin/lesspipe %s
LESSCLOSE=/usr/bin/lesspipe %s %s
_=/usr/bin/env
OLDPWD=/root
2
  • run env from your terminal, and check what the classpath is set to there. Commented Jun 15, 2014 at 17:35
  • Thanks for help, I have added the output env gives me. Commented Jun 15, 2014 at 17:39

1 Answer 1

1

Seems to me like your apache server user does not have access to the location

/root/weka-3-6-11/weka.jar

You can verify this by running

filesize("/root/weka-3-6-11/weka.jar")

from within PHP.

Any program started by exec will run as the same user as the apache server user - make sure that you set the permissions on your jar file and its parent directory so that the apache server user (usually user www-data) can read it.

Sign up to request clarification or add additional context in comments.

2 Comments

I have thought of permissions before. And I think you are right. filesize() does not output anything, so I think it does not have the rights. Could you help me set them? I'm not so experienced with setting permissions.:) And yes, the user is www-data.
You could modify permissions, but maybe the best way is to put the file in a location where it can be read? Try creating a directory somewhere /usr/share/weka and set run the following chmod 755 -R /usr/share/weka

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.