Help php compile javac or java system environment variable

Whether you are using Linux on the desktop or as a server, it's still good that you're using Linux. Linux related questions go here.

Moderator: General Moderators

Post Reply
antonycsf
Forum Newbie
Posts: 16
Joined: Mon Jun 14, 2004 3:29 am

Help php compile javac or java system environment variable

Post by antonycsf »

can anyone tell me how to set the environment variable on linux so that it can just run the java or javac command only!!

=============================
/usr/java/j2sdk1.4.2_04/bin/java HelloWorld
|
V
java HelloWorld
=============================

I have append the line at /etc/profile
#set java environment
export PATH=$PATH:/usr/java/j2sdk1.4.2_04/bin
export JAVA_HOME=/usr/java/j2sdk1.4.2_04
export CLASSPATH=/usr/java/j2sdk1.4.2_04/lib:$CLASSPATH:.





the following code works but I don't understand need to specify the whole path to run java and javac at php

$last = exec("/usr/java/j2sdk1.4.2_04/bin/javac $filename", $output);
$last = exec("/usr/java/j2sdk1.4.2_04/bin/java HelloWorld",$output);


Code: Select all

<?php
<?php 

if(!defined('_DEBUG_')) define('_DEBUG_',1); 

if(_DEBUG_) 
{ 
header('Content-type: text/plain'); 

ini_set('display_errors','1'); 
error_reporting(E_ALL); 
} 
$filename = "HelloWorld.java"; 
$last = exec("/usr/java/j2sdk1.4.2_04/bin/javac $filename", $output);
if(_DEBUG_) 
{ 
echo $last."\n"; 
print_r($output); 
} 

$last = exec("/usr/java/j2sdk1.4.2_04/bin/java HelloWorld",$output);
if(_DEBUG_) 
{ 
echo $last."\n"; 
print_r($output); 
} 

?>
?>

Thx !! :oops: :oops: :oops: :oops:
User avatar
scorphus
Forum Regular
Posts: 589
Joined: Fri May 09, 2003 11:53 pm
Location: Belo Horizonte, Brazil
Contact:

Post by scorphus »

I'll try to help in the environment variable issue first. Place this on /etc/profile:

Code: Select all

# Java Setup Section BEGIN
export JAVA_HOME="/usr/java/j2sdk1.4.2_04"
export JRE="$JAVA_HOME/jre"
export CLASSPATH="$JAVA_HOME/lib:$JRE/lib:$JAVA_HOME/lib/tools.jar::."
export PATH="$PATH:$JAVA_HOME/bin"
# Java Setup Section END
and I think you will get it working.

-- Scorphus
antonycsf
Forum Newbie
Posts: 16
Joined: Mon Jun 14, 2004 3:29 am

Post by antonycsf »

beginning or at end
antonycsf
Forum Newbie
Posts: 16
Joined: Mon Jun 14, 2004 3:29 am

Post by antonycsf »

I have try add to beginning and at the end
the result is also command not found [/var/log/httpd/error_log]
User avatar
scorphus
Forum Regular
Posts: 589
Joined: Fri May 09, 2003 11:53 pm
Location: Belo Horizonte, Brazil
Contact:

Post by scorphus »

I got it running, take a look:

javac.php:

Code: Select all

<?php
header('Content-type: text/plain');
ini_set('display_errors','1');
error_reporting(E_ALL);
$filename = "HelloWorld.java";
$last = exec("/opt/j2sdk_nb/j2sdk1.4.2/bin/javac $filename", $output);
echo $last."\n";
print_r($output);
$last = exec("/opt/j2sdk_nb/j2sdk1.4.2/bin/java HelloWorld",$output);
echo $last."\n";
print_r($output);
?>
HelloWorld.php (taking advantage of syntax highlighting):

Code: Select all

public class HelloWorld {
	public static void main (String args[]) {
		System.out.println("This the hello world test!");
	}
}
testing on command-line:

Code: Select all

&#1111;~/lab/php/lab] on Fri Jun 18, 01:02:12 - 0
&#1111;scorphus]@&#1111;Kilauea] $ php -f javac.php

Array
(
)
This the hello world test!
Array
(
    &#1111;0] =&gt; This the hello world test!
)
&#1111;~/lab/php/lab] on Fri Jun 18, 01:02:15 - 0
&#1111;scorphus]@&#1111;Kilauea] $
and webserver:

Code: Select all

Array
(
)
This the hello world test!
Array
(
    &#1111;0] =&gt; This the hello world test!
)
Sorry I can't show you this running. My server, an ancient Pentium 90 MHz with 16 MB of RAM, is already under stress with almost 85% of VM and stalls when it tries to run Java.

Well,
- What GNU/Linux distro is it running on?
- How did you install the J2SE and RE on the system?
- Please show us the error log lines.

-- Scorphus
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Post by redmonkey »

I think it has already been established that that the commands can be run by specifying the full path. I assume the goal is to call it like....

Code: Select all

$last = exec("javac $filename", $output);
And...

Code: Select all

$last = exec("java HelloWorld",$output);
It looks like the PATH variable is not being set in the correct place, try running....

Code: Select all

passthru('echo $PATH');
Which should show your current PATHs available, I'm betting that the paths to the java stuff are not there.

The system wide PATH is generally set in /etc/profile or /etc/bashrc it may be in another file, you should check with your distros documentation. If worst comes to worst, you could always use symlinks to get there.
Post Reply