Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / July 2005

Tip: Looking for answers? Try searching our database.

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

Thread view: 
lbrtchx@hotmail.com - 28 Jul 2005 20:00 GMT
// __
[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?
lbrtchx@hotmail.com <Albretch Muller>
Raymond DeCampo - 28 Jul 2005 21:08 GMT
> // __ 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):
> ...
[quoted text clipped - 7 lines]
>  How do you get the errors from the compilation tried within a running JVM?
> lbrtchx@hotmail.com <Albretch Muller>

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

HTH,
Ray

Signature

XML is the programmer's duct tape.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.