ANOMALIES TRYING TO COMPILE JAVA CODE FROM WITH IN A RUNNING JAVA APPLICATION

L

lbrtchx

// __
[root@localhost XSD02]# javac javac02.java
javac02.java:33: illegal escape character
aCmmnd = new String[]{"echo","\$PATH"};

// __ 1. java reachable from within my box' PATH

[root@localhost XSD02]# echo $PATH
/usr/kerberos/sbin:
/usr/kerberos/bin:
/usr/local/sbin:
/usr/local/bin:
/sbin:/bin:
/usr/sbin:
/usr/bin:
/usr/X11R6/bin:
/opt/jdk/jdk1.5.0_03/bin:
/usr/local/jwsdp-1_6/jaxb/bin:
/usr/local/ant/apache-ant-1.6.5/bin:
/root/bin

[root@localhost bin]# pwd
/opt/jdk/jdk1.5.0_03/bin
[root@localhost bin]# ls -l java*
-rwxr-xr-x 1 root root 64492 Apr 13 06:55 java
-rwxr-xr-x 1 root root 71632 Apr 13 06:55 javac
-rwxr-xr-x 1 root root 71600 Apr 13 06:57 javadoc
-rwxr-xr-x 1 root root 71600 Apr 13 06:57 javah
-rwxr-xr-x 1 root root 71600 Apr 13 07:29 javap
-r-xr-xr-x 1 root root 1789 Apr 13 07:27 java-rmi.cgi
-rwxr-xr-x 1 root root 175978 Apr 13 07:54 javaws

// __ 2. you can compile java source files from the command prompt, without any
problems

// __ 3. you can make system/OS's calls from a java source file

import java.io.*;
import java.util.*;

class SysExec00{
private Runtime RT;
// __
SysExec00(){ RT = Runtime.getRuntime(); }
// __
public String[] exec(String[] aCmmnd){
ArrayList<String> ALS = new ArrayList<String>();
String[] aSAr = null;
// __
try{
Process Prx = RT.exec(aCmmnd);
BufferedReader BR = new BufferedReader(new InputStreamReader(Prx.getInputStream()));
String aLn = BR.readLine();
while(aLn != null){ ALS.add(aLn); aLn = BR.readLine(); }
}catch(IOException IOX){ IOX.printStackTrace(); }
// __
int iSz = ALS.size();
if(iSz > 0){ aSAr = new String[iSz]; ALS.toArray(aSAr); }
// __
return(aSAr);
}
}

// __
public class javac02{
public static void main(String[] aArgs){

// __ OS calls are fine
String[] aOut;
String[] aCmmnd;
SysExec00 SEx = new SysExec00();
// __
aCmmnd = new String[]{"ps","-aux"};
aOut = SEx.exec(aCmmnd);
if((aOut != null) && (aOut.length > 0)){
for(int k = 0; (k < aOut.length); ++k){
System.out.println("// __ aOut[" + k + "]: |" + aOut[k] + "|");
}
}// ((aOut != null) && (aOut.length > 0))

// __
aCmmnd = new String[]{"ls","-l"};
aOut = SEx.exec(aCmmnd);
if((aOut != null) && (aOut.length > 0)){
for(int k = 0; (k < aOut.length); ++k){
System.out.println("// __ aOut[" + k + "]: |" + aOut[k] + "|");
}
}// ((aOut != null) && (aOut.length > 0))

/*
// __ how to echo an environment variable?
aCmmnd = new String[]{"echo","$PATH"};
aOut = SEx.exec(aCmmnd);
if((aOut != null) && (aOut.length > 0)){
for(int k = 0; (k < aOut.length); ++k){
System.out.println("// __ aOut[" + k + "]: |" + aOut[k] + "|");
}
}// ((aOut != null) && (aOut.length > 0))
*/

// __ but trying to compile a class fails
aCmmnd = new String[]{"javac", "./test00.java"};
aOut = SEx.exec(aCmmnd);
if((aOut != null) && (aOut.length > 0)){
for(int k = 0; (k < aOut.length); ++k){
System.out.println("// __ aOut[" + k + "]: |" + aOut[k] + "|");
}
}// ((aOut != null) && (aOut.length > 0))
}
}

// __ 4. however trying to compile this simple piece of code:

import java.util.*;

public class test00{
test00(){}
// __
public String getTime(){ return(new Date()); }
// public String getTime(){ return(new Date().toString()); }
// __
public static void main(String[] aArgs){
test00 t = new test00();
System.err.println(t.getTime());
}
}

gives you:
[root@localhost XSD02]# javac test00.java
test00.java:6: incompatible types
found : java.util.Date
required: java.lang.String
public String getTime(){ return(new Date()); }
^
1 error

// __ 5. BUT NO ERRROS ARE SHOWN WHEN YOU DO THE COMPILING FROM WITHIN A RUNNING JAVA APP CALLING as tried through (see code snippet above):
....
aCmmnd = new String[]{"javac", "./test00.java"};
aOut = SEx.exec(aCmmnd);
....
NO ERRORS ARE REPORTED

// __ 6. fixing the code will allowed for the compilation of the class

How do you get the errors from the compilation tried within a running JVM?
(e-mail address removed) <Albretch Muller>
 
R

Raymond DeCampo

// __ 5. BUT NO ERRROS ARE SHOWN WHEN YOU DO THE COMPILING FROM WITHIN A RUNNING JAVA APP CALLING as tried through (see code snippet above):
...
aCmmnd = new String[]{"javac", "./test00.java"};
aOut = SEx.exec(aCmmnd);
...
NO ERRORS ARE REPORTED

// __ 6. fixing the code will allowed for the compilation of the class

How do you get the errors from the compilation tried within a running JVM?
(e-mail address removed) <Albretch Muller>

It looks like you do not read from the standard error stream of the
child process, only the standard output.

HTH,
Ray
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top