• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

compile at run time????

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hai,
In my work my servlet generates a java class file say "abc.java" , this java file have to be compiled at runtime and this compiled class has to be used in the further execution with out stopping my servlet runner.
this Generated "abc.java" may change in every request.
I am using servlet runner and java web server 2.0.
`````````````````````````````````````````````````````
Just to test this i have written a servlet testExe.java ,class getFileData.java ,GotoClass.java, WriteIntoFle.java
Initially i compile all of them and run the servlet, after that i make change in the GotoClass.java in str variable(now i don't compile it). and run the servlet.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class testExe extends HttpServlet{
public void init(ServletConfig config) throws ServletException {
super.init(config);
} // End of init()
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws IOException, ServletException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();

out.println(" <html><head><title> Hai</title></head><body>");
String str = " Hello";
out.println("<b>"+str+"</b><br>");
getFileData gt = new getFileData();

String data = gt.getfiledata();
System.out.println(" File Received to Servlet from getFileData:\n");
System.out.println(" Data:\n"+data+"\n Data Complete");
out.println("data printing :<br>"+data+"<br> Data printing Completed");

WriteIntoFile wf = new WriteIntoFile();
wf.writefile("GotoClass.java",data);
try{
Process p = Runtime.getRuntime().exec ("javac GotoClass.java");
System.out.println("Compiled");
p.destroy();
System.out.println("Operation Complete");
System.out.println(" Before the use of the class");
GotoClass gc = new GotoClass();
String str1 = gc.gotoclass();
out.println("<b>gc.gotoclass :"+str1+"</b>");
}//try
catch(IOException ioe){
ioe.printStackTrace();
}// catch
out.println("</body></html>");
out.close();
}// end of doGet()
}// end of class testExe
----------------------------------------------------------------
import java.io.*;
public class getFileData{
public String getfiledata(){
String retdata = null;
try{
System.out.println("\n\n Execution in getFileData:");
FileInputStream in = new FileInputStream("GotoClass.java");
InputStreamReader isr = new InputStreamReader(in);
int ch=0;
StringBuffer buf = new StringBuffer();
while((ch = in.read())> -1){
buf.append((char)ch);
System.out.print((char)ch);
}

System.out.println("\n\n");
retdata = buf.toString();
System.out.print("printing file :\n"+retdata+"\n Data Complete\n");
}
catch (IOException e){
System.out.println(e.getMessage());
}
System.out.println(" Termination of getFileData:\n\n");
return retdata;
}// end of method

}// end of class
----------------------------------------------------------------
import java.io.*;
import java.util.*;
import java.net.*;
public class WriteIntoFile {
public void writefile(String filename, String data){

System.out.println("\n\nExecution in WriteIntoFile:\n");
System.out.println("File name:\t"+filename+"\t \n Data received :\n"+data);
String pathname ="f:/mtp/temp/files/"+filename;
System.out.println(" Path name :\t "+ pathname);
File f = new File(pathname);
String s=null;
if(f.exists()){
System.out.println("File is all ready there");
f.delete();
f = new File(pathname);
try{
boolean created=false;
created=f.createNewFile();
System.out.println("After the File Creation");
}// end of try
catch(IOException ioe){
System.out.println("File Creation Error:\t\n");
ioe.printStackTrace();
}// end of catch
if(f.exists()){
String s1 = f.getAbsoluteFile().getName();
s = f.getName();
System.out.println(" File Name Created :\t"+ s);
System.out.println(" s1:\t"+s1);
s1 = f.getPath();
System.out.println(" Path of the file :\t"+s1);
System.out.println(" File Url Name:\t"+s1);
try{
FileWriter fw = new FileWriter(s);
String str = data;
fw.write(str);
fw.flush();
fw.close();
}
catch(IOException ioe){
ioe.printStackTrace();
}
}// end of if exists
}// end of if(f.exists())

else {
try{
boolean created=false;
created=f.createNewFile();
System.out.println("After the File Creation");
}// end of try
catch(IOException ioe){
System.out.println("File Creation Error:\t\n");
ioe.printStackTrace();
}// end of catch
if(f.exists()){
String s1 = f.getAbsoluteFile().getName();
s = f.getName();
System.out.println(" File Name Created :\t"+ s);
System.out.println(" s1:\t"+s1);

try{
FileWriter fw = new FileWriter(s);
String str = data;
fw.write(str);
fw.flush();
fw.close();
}
catch(IOException ioe){
ioe.printStackTrace();
}
}// end of if exists
}// end of esle if(f.exists())
System.out.println("\nTermination of WriteIntoFile file\n\n\n");
}// end of writefile method

}// end of class WriteIntoFile
----------------------------------------------------------------
import java.io.*;
import java.util.*;
public class GotoClass {
public String gotoclass(){
//String str =" Before making change, <b>Initially compiled<br></b><br>No change in execution";
String str = " After the compilation, <b>Compiled at runtime<br></b> Changes Done, Congrats";
return str;
}
}
--------------------------------------------------------------

thanx
prabhakar.
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have my doubts about this working. Depending on class loading to reflect dynamic changes is not bound to have problems and platform/JVM-specific dependencies. Analyze what you are trying to do and try and do it by providing an API that does what you want.
Also, if you need to, look at the using the Reflection APIs ...
Specifically, what are you trying to accomplish with this code?
 
varkala prabhakar
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hai bill,
the above given code is a trail. actually in my project with the available enterprise data i generate java class dynamically (used in data mining and intelligence part). this has to be complied and used in further comming request.
thanx
prabhakar
 
author
Posts: 3892
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
While generating and compiling classes is a commonly used thing in languages like Smalltalk, I have my doubts about whether or not you should try it in Java. It sounds like a massive security hole...
Why not try using something like the Interpreter pattern or a rules engine to accomplish the same thing?
Kyle
------------------
Kyle Brown,
Author of Enterprise Java (tm) Programming with IBM Websphere
See my homepage at http://members.aol.com/kgb1001001 for other WebSphere information.
 
I AM MIGHTY! Especially when I hold this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic