can not read or write to a file on the server side

57 views
Skip to first unread message

HDayi

unread,
Apr 6, 2010, 8:58:11 AM4/6/10
to Google Web Toolkit
hi there,
on the server side, i am trying to read some string from a file and
write it back to file again. I am using eclipse. on the eclipse, i put
my text file to war directory and everything works fine. but when i
upload my application it doesn't work but my async callback does not
fail.

i couldn't understand what my problem is.

kozura

unread,
Apr 6, 2010, 9:03:18 AM4/6/10
to Google Web Toolkit
Likely you need to configure Java permissions for things like
accessing files on your tomcat/jetty deployment server; look at the
log files under these and you should be able to figure out the
problem. You can also put debugging printouts in your servlet and
find them in these logs.

Sripathi Krishnan

unread,
Apr 6, 2010, 9:12:30 AM4/6/10
to google-we...@googlegroups.com
Are you using Google App Engine?
GAE does not allow you to create new files. You can only read from the files that are present in your war file.

If you don't use GAE, you may have accidentally enabled the 'Use GAE' setting in eclipse. That setting is likely to cause the errors you describe.

--Sri



--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To post to this group, send email to google-we...@googlegroups.com.
To unsubscribe from this group, send email to google-web-tool...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.


HDayi

unread,
Apr 6, 2010, 9:22:02 AM4/6/10
to Google Web Toolkit

hi again,
i am using tomcat5 and it says:
blabla.txt (No such file or directory)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:137)
at java.io.FileInputStream.<init>(FileInputStream.java:96)
and so on ....

i will check the java permissions

kozura

unread,
Apr 6, 2010, 9:35:36 AM4/6/10
to Google Web Toolkit
Doesn't sound like permissions (yet), more about how you are accessing
the file. A servlet doesn't access files by their full path, but as
Resources, google servlet getResource.

HDayi

unread,
Apr 6, 2010, 9:44:07 AM4/6/10
to Google Web Toolkit
i am trying to open the file with the fallowing code:

BufferedReader input = new BufferedReader(new
FileReader("blabla.txt"));

it works while developping with eclipse.
and i am trying to write whit the fallowing:

BufferedWriter out = new BufferedWriter(new FileWriter("blabla.txt"));
out.write(liste);
out.close();

both works fine on eclipse.
when i upload to server the first one gives a long error message
saying "file not found" in tomcat log file as i mentioned before.
second one gives onli on word in tomcat log "Exception".

andrew19881123

unread,
Apr 6, 2010, 10:09:46 AM4/6/10
to Google Web Toolkit
i have a similar problem with an xml file that i try to read..
the code for read is:
try {
File fileXml = new File("./vini.xml");
System.out.println("hi");
System.out.println("owner's write permission :
"+fileXml.setWritable(true)); //exception cached
System.out.println("owner's read permission :
"+fileXml.setReadable(true));
...
}
catch (Exception e) {
lst += e; //browser output
e.getStackTrace(); //console output
}

while debugging the output in the browser is:
java.security.AccessControlException: access denied
(java.io.FilePermission ./vini.xml write)
if i don't set the fileXml.setWritable(true) it works fine! but not
when i deploy the app in tomcat6

where can i set the permissions?
i tried to add this
grant codeBase "file:${catalina.home}/bin/tomcat-juli.jar" {
permission java.security.AllPermission;
};
in /etc/tomcat6/policy.d/03catalina.policy but it doesn't work....

Lothar Kimmeringer

unread,
Apr 6, 2010, 10:26:24 AM4/6/10
to google-we...@googlegroups.com
HDayi schrieb:

> hi again,
> i am using tomcat5 and it says:
> blabla.txt (No such file or directory)
> at java.io.FileInputStream.open(Native Method)
> at java.io.FileInputStream.<init>(FileInputStream.java:137)
> at java.io.FileInputStream.<init>(FileInputStream.java:96)
> and so on ....

Give out new File("blabla.txt").getAbsolutePath() and
make sure that the location java.io.File thinks you
are meaning is actually the location you intend to
access.


Regards, Lothar

HDayi

unread,
Apr 6, 2010, 10:41:58 AM4/6/10
to Google Web Toolkit
hi again,

i could solve the reading problem with the fallowing code.

ServletContext sc = this.getServletConfig().getServletContext();
InputStream is = sc.getResourceAsStream("/blabla.txt");
BufferedReader input = new BufferedReader(new InputStreamReader(is));

when i use the lothar's way i saw that it's serching the file in "/usr/
share/tomcat5/" directory.
so if i use only the file name it doesn't search in the application
directory.

HDayi

unread,
Apr 6, 2010, 11:13:25 AM4/6/10
to Google Web Toolkit
hi again and again

ServletContext sc = this.getServletConfig().getServletContext();


BufferedWriter out = new BufferedWriter(new

FileWriter(sc.getRealPath("/blabla.txt")));

with this code writing problem is also solved.

but there is one point to notice: tomcat user must have the write
permission on the file.

thank you everyone

andrew19881123

unread,
Apr 6, 2010, 5:50:38 PM4/6/10
to Google Web Toolkit
I tried in a lot of way to read an external file, but i continue to
have the same problem with permission..
i can read the file with jetty but not with tomcat..
can someone post me a guide or a stupid tutorial??

thx

Message has been deleted

andrew19881123

unread,
Apr 7, 2010, 8:18:49 AM4/7/10
to Google Web Toolkit
now have done in this way..

try {
String fileXml = new String();
String s = new String();
FileReader f = new FileReader("vini.xml");

BufferedReader br = new BufferedReader(f);

do {
s = br.readLine();
if (s!=null)
fileXml=s;
} while (s!=null);

it works while debugging.. but when i deploy:
java.io.FileNotFoundException: vini.xml (No such file or
directory)java.io.FileNotFoundException: vini.xml (No such file or
directory)[Ljava.lang.StackTraceElement;@115126e
the file is present in this dir /var/lib/tomcat6/webapps/rpcTest0..
and i can't get the absolute path of f because there is no method.. grr

Message has been deleted

andrew19881123

unread,
Apr 7, 2010, 9:29:37 AM4/7/10
to Google Web Toolkit
the file is perfectly localized in the correct directory, i know
because the debug in eclipse works...!! it's here /var/lib/tomcat6/
webapps/rpcTest0 where rpcTest0 is the project.. boh

i'm from italy.. near modena, im a student of computer science at
unibo, it's about 3 weeks that i try to learn gwt cause i will have a
project for the university that need it.. this is just an exercise to
make practice in preparation of... the whole exercise is to read an
xml file and write it in the browser.. but if i can't read it i can't
go ahead.. uff

and what about you? are u italian too? whats your occupation?

On Apr 7, 2:36 pm, Giovanni Gaglione <giovanni.gagli...@gmail.com>
wrote:
> It seems that application doesn't find the file.
> Where is your file?
> It's possible that you must specify the file path better.
>
> Where are you from?
> What are you doing with GWT?
> Who are you?
>
> regards
>
> 2010/4/7 andrew19881123 <andrew19881...@gmail.com>

> > --
> > You received this message because you are subscribed to the Google Groups
> > "Google Web Toolkit" group.
> > To post to this group, send email to google-we...@googlegroups.com.
> > To unsubscribe from this group, send email to

> > google-web-tool...@googlegroups.com<google-web-toolkit%2Bunsubs cr...@googlegroups.com>

Message has been deleted

andrew19881123

unread,
Apr 7, 2010, 10:03:19 AM4/7/10
to Google Web Toolkit
il mio e' un progetto x tecnologie web.. anche io sn alla triennale..
i permessi di linux li ho settati senza problemi.. ho provato agirare
un po su google x cercare come "sbloccare" i file su tomcat.. e ho
letto che c'e' da aggiungere grant{...} dentro alle policy solo che nn
ho concluso nnt..

avevo provato anche io a creare un file.. cosi' da vedere dv cavolo lo
mette e d conseguenza c piazzo il mio xml.. l'avevo fatto cosi':
File file = new File("test.txt");
if (file.exists()) {
file.delete();
}
try {
file.createNewFile();
} catch (IOException e1) {
e1.printStackTrace();
}
System.out.println("Before. canWrite?" + file.canWrite());

// set to read-only, atau canWrite = false
file.setWritable(false);
System.out.println("After. canWrite?" + file.canWrite());

il problema e' che sempre x colpa dei permessi java nn puo scrivere
nella cartella..
oltretutto ho provato anche un altro metodo x leggere un file, solo
che in seguito al metodo canWrite() scatta l'eccezione e nn capisco il
motivo dato che dovrebbe essere solo un si o un no.. vedi se riesci a
creare il file con quel codice e poi fammi sapere

ciao

On Apr 7, 3:52 pm, Giovanni Gaglione <giovanni.gagli...@gmail.com>
wrote:
> auhauha
> Io pure sono italiano!!!!
> di roma però.
> sto facendo la tesi per la laurea triennale in informatica...e utilizzo gwt
> da un mesetto o di più.
>
> Hai settato i permessi per il filesystem come ti avevo suggerito?
> Se il file non ha il permesso di essere letto, tomcat non lo trova.
>
> Che io sappia, ci stanno anche i permessi di tomcat sui file, magari
> potrebbe dipendere da questo.
> Se sgoogli un pochino, potresti vedere se è il caso tuo, vedendo come si
> settano i permessi di tomcat.
>
> Usi firebug? ti può convenire usarlo per vedere nel codice se è tutto
> aposto, se c'è qualche errore javascript, non so se mi spiego.
>
> Ma cosa ci stai facendo con gwt?
> Sai per caso come creare un file lato server? ti è capitato di fare un
> operazione simile con gwt?
>
> saluti
>
> 2010/4/7 andrew19881123 <andrew19881...@gmail.com>

> > > > google-web-tool...@googlegroups.com<google-web-toolkit%2Bunsubs cr...@googlegroups.com><google-web-toolkit%2Bunsubs

bitliner

unread,
Apr 7, 2010, 10:07:37 AM4/7/10
to Google Web Toolkit
Non posso provare ora il codice, nè penso stasera.
Troppo impegnato.
E' probabile che devi configurare meglio tomcat, ma non hai trovato
nulla di qualcuno che affrontasse il tuo stesso problema?
Indipendentemente da gwt?

non so come altro aiutarti

saluti

andrew19881123

unread,
Apr 7, 2010, 11:12:23 AM4/7/10
to Google Web Toolkit
ma guarda.. gwt o no cmq sto programmando in java su lato server con
le api d java x cui e' come se nn lo usassi proprio... sto odiando
tomcat...

HDayi

unread,
Apr 8, 2010, 7:41:35 AM4/8/10
to Google Web Toolkit
hi andrew,
when you give only the file name, tomcat does not use your application
directory to access it. it uses it's own installation directory. so
you must use the exact path of the file or use ServletContext as i
mentioned abow.
shortly you must do fallowing:
for reading the file:

ServletContext sc = this.getServletConfig().getServletContext();
InputStream is = sc.getResourceAsStream("/blabla.txt");
BufferedReader input = new BufferedReader(new
InputStreamReader(is));

then you can acces your file correctly.

while wrting the file:


ServletContext sc = this.getServletConfig().getServletContext();
BufferedWriter out = new BufferedWriter(new
FileWriter(sc.getRealPath("/blabla.txt")));

if you are using linux, tomcat user must have read and write rights of
the file.

good luck

andrew19881123

unread,
Apr 8, 2010, 8:01:33 AM4/8/10
to Google Web Toolkit
yeeeeeeeah it works!!!!! thaks thaks thaks thaks thaks thaks
thaks!!!! :D

andrew19881123

unread,
Apr 8, 2010, 4:36:29 PM4/8/10
to Google Web Toolkit
ok, now i have to do the same in the client-side.. there is no method
getServletConfig()
how can i do?

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To post to this group, send email to google-we...@googlegroups.com.
To unsubscribe from this group, send email to google-web-tool...@googlegroups.com.

Katharina Probst

unread,
Apr 8, 2010, 4:42:18 PM4/8/10
to google-we...@googlegroups.com
Well, your app won't just be able to access the file system on the client side, because the browser runs in a sandbox (you wouldn't want any web app reading your files from your desktop...).  But you *could* have the user upload a file, if that's what you want?  See http://google-web-toolkit.googlecode.com/svn/javadoc/2.0/com/google/gwt/user/client/ui/FileUpload.html.

kathrin

andrew19881123

unread,
Apr 8, 2010, 5:30:49 PM4/8/10
to Google Web Toolkit
mmm.. it's strange.. neither if i make an HTTP GET request with a
RequestBuilder like this?

RequestBuilder xmlFile = new RequestBuilder(RequestBuilder.GET, "/
blabla.xml");

try {
xmlFile.sendRequest(null, new RequestCallback() {
public void onError(Request request, Throwable exception) {
requestFailed(exception);
}
public void onResponseReceived(Request request, Response response)
{
parseXML(response.getText());
}
});
} catch (RequestException ex) {
requestFailed(ex);
}

On Apr 8, 10:42 pm, Katharina Probst <kpro...@google.com> wrote:
> Well, your app won't just be able to access the file system on the client
> side, because the browser runs in a sandbox (you wouldn't want any web app
> reading your files from your desktop...).  But you *could* have the user
> upload a file, if that's what you want?  Seehttp://google-web-toolkit.googlecode.com/svn/javadoc/2.0/com/google/g...
> .
>
> kathrin

bradrover

unread,
Apr 8, 2010, 10:48:16 PM4/8/10
to Google Web Toolkit
Just put your xml file in a folder under your war directory, like war/
data/blah.xml, then in your RequestBuilder use a path like "data/
blah.xml". Don't use a slash before the path. You really don't need
server side code for this.

Brad

andrew19881123

unread,
Apr 9, 2010, 2:01:43 PM4/9/10
to Google Web Toolkit
ih... i have a problem again in writing in a file.. i'm sorry but i'm
a newbie in GWT

[...]
import java.io.FileWriter;
[...]
private void postFile(String fileXml) {
try {


ServletContext sc = this.getServletConfig().getServletContext();
BufferedWriter out = new BufferedWriter(new

FileWriter(sc.getRealPath("/blabla.xml")));
} catch (Exception e) {
e.getStackTrace();
}
}

i have the following error: in FileWriter: java.io.FileWriter is not
supported by GAE's JRE. Why?
i need to complete this method for writing in the file a String
fileXml.. how can i do?

kozura

unread,
Apr 9, 2010, 2:46:38 PM4/9/10
to Google Web Toolkit
See third reply, GAE doesn't support writing new files..use the data
store instead:

http://code.google.com/appengine/kb/java.html#writefile

Reply all
Reply to author
Forward
0 new messages