Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

permission denied error while creating a file

 
rahul takkallapally
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi ,
i am trying to create a file using a servlet , this servlet is invoked form an applet , i am getting an exception saying permission denied.. , the code of my program is ...
try {
File outputFile = new File("window_surf_test");
FileOutputStream fos = new FileOutputStream(outputFile);
PrintWriter pwriter = new PrintWriter(fos);
pwriter.println(stringValue1);
pwriter.println(stringValue2);
pwriter.println(stringValue3);
//pwriter.println(f);
System.out.println( " Check the result: " + stringValue2 );
pwriter.close();
fos.close();
} catch (FileNotFoundException e) {
System.err.println("FileStreamsTest: " + e);
stringValue1=e.toString();
}

Please tell me how to resolve this problem .......,
thaks ......
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
windows or linux?

what's the full stack trace say? With I/O errors, there is normally a helpful human-readable message somewhere in the stacktrace.

You're trying to create the file on the server, right?
 
rahul takkallapally
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yup i am trying to create the file on the server itself , and i put an exception for file not found so i am getting the exception as
file not found : file name , permission denied..
the second question is how do i mention the path where to create the file???

thanks a lot in advance
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if you're on linux, ensure that the directory permissions allow you to create and modify files.

you specify the path when you create the File object. Send it an absolute path, rather than just the name of the file.
 
Tim Baker
Ranch Hand
Posts: 541
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
also, the servlets may be sandboxed depending on your tomcat install. you may have to edit the .policy files in your tomcat/conf folder to allow writing of files in whatever directory you want!
and to get a relative path you must do this
BufferedReader br = new BufferedReader(new FileReader(getServletContext().getRealPath("WEB-INF/blah.txt")));
if you want an absolute one you do this
BufferedReader br = new BufferedReader(new FileReader("/home/blah/blah.txt"));
just change classes for a writer etc.
 
rahul takkallapally
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi ,
i changed the permissions of the directory , and gave an absolute path too , but its not working, i am still getting the permisssion deined error. , i am using a unix machine... , the code for creation of the file is something like this..
try {
File outputFile = new File("/usr/internet/httpd/tomcat/webapps/examples/servlets/window_surf_test");
FileOutputStream fos = new FileOutputStream(outputFile);
PrintWriter pwriter = new PrintWriter(fos);
pwriter.println(stringValue1);
pwriter.println(stringValue2);
pwriter.println(stringValue3);
//pwriter.println(f);
System.out.println( " Check the result: " + stringValue2 );
pwriter.close();
fos.close();
} catch (FileNotFoundException e) {
System.err.println("FileStreamsTest: " + e);
stringValue1=e.toString();
}

can u tell me what part of the policy file should i change , i am using tomcat version 1.1
please help

thanks.....
 
Tim Baker
Ranch Hand
Posts: 541
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1.1? woah, im not sure how 1.x works, but in 4.x you add something like this
grant codeBase "file:/home/virtual/site20/fst/var/www/html/-" {
permission java.security.AllPermission;
};
obviously changing /home/virtual.... to the directory you want to edit.
 
rahul takkallapally
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks a lot for ur replys..........
i went into the conf file and made changes of this sort ,
<Directory "/usr/apache/...">
Options Indexes MultiViews FollowSymLinks
AllowOverride All
</Directory>
it dint work ,i am still getting the permission denied error, can u tell me is what i did right or is there some other way of doing it
please help
thnaks
 
rahul takkallapally
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks a lot every body
i got the solution .........
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic