Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

JSP reading file path problem(relative path and absolute path)

843836Feb 16 2004 — edited Feb 16 2004
Hello!

I am trying to call a bean from a web page to get some results. For example in webpage.jsp I have:

<jsp:useBean id="mybean" scope="request" class="hello.NameHandler" />
<jsp:setProperty name="mybean" property="*" />

<%= mybean.readFile() %>
<%= mybean.compile() %>

In the NameHandler.java I am supposed to read a java file - Sieve.java, which is located under the path /var/http/tomcat/webapps/testing/WEB-INFO/classes/hello/, return the content and also compile it. But the problem is with relative path (filename2 in readFile()), the file cannot be read (it gives me FileNotFound exception). However if I use absolute path (filename1 in readFile()), the jave file is read in, but then I cannot compile the java program(it gives me Nullpointerexception). Here are the two methods in NameHandler.java. The file hierachy is as follows:
ls
NameHandler.java NameHandler.class sourcecode
ls sourcecode
Sieve.java Sieve.class
package hello;

public class NameHandler{
........ //constructors

public String compile(){
String cmdline=null;
String msg="";
Runtime rt;
Process child;
InputStreamReader pout;
BufferedReader whoout;
try{
// run the standard program
cmdline = "java -cp sourcecode Sieve";
rt = Runtime.getRuntime();
child = rt.exec(cmdline);
child.waitFor();
pout = new InputStreamReader(child.getInputStream() );
whoout = new BufferedReader(pout);
msg = whoout.readLine();
}
catch(Exception e) { msg= e.toString(); }
}

public String readFile(){
String line="";
String filename1 = "/var/httpd/tomcat/webapps/testing/WEB-INF/classes/hello/sourcecode/Sieve.java";
String filename2 = "sourcecode/Sieve.java";
BufferedReader br;
String newline = System.getProperty("line.separator");
String cmdline=null;
Runtime rt;
Process child;
try{
br = new BufferedReader(new FileReader(filename1));

String re;
do{
re= br.readLine();
if(re != null)
line += re;
} while (re != null);
br.close();
}
catch( FileNotFoundException fe){}
catch(IOException ie){}
catch(Exception e){}
return line;
}


Thanks a lot in advance.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 15 2004
Added on Feb 16 2004
2 comments
535 views