gopikrish
asked on
Cant write to a RandomAccessFile?
When I click loginbutton in the applet many errors are occuring and nothing is written to the file "Customer.txt"
The code is as follows...
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
//<applet code=Login.class Width=400 Height=400>
//</applet>
public class Login extends JApplet implements ActionListener
{
JTextField textcustname;
JPasswordField textpassword;
JLabel labelcustname;
JLabel labelpassword;
JButton loginbutton;
public void init()
{
createApplet();
}
public void createApplet()
{
Container content;
content=getContentPane();
content.setLayout(new FlowLayout());
labelcustname=new JLabel("Customer Login Name:");
textcustname=new JTextField(10);
labelpassword=new JLabel("Password:");
textpassword=new JPasswordField(10);
loginbutton=new JButton("Login");
content.add(labelcustname) ;
content.add(textcustname);
content.add(labelpassword) ;
content.add(textpassword);
content.add(loginbutton);
loginbutton.addActionListe ner(this);
}
public void actionPerformed(ActionEven t evt)
{
Object obj=evt.getSource();
if(obj==loginbutton)
{
String entry=textcustname.getText () +":"+new String(textpassword.getPas sword());
try
{
RandomAccessFile logfile=new RandomAccessFile("D:\\java \\bin\\Cus tomer.txt" ,"rw");
logfile.seek(logfile.lengt h());
logfile.writeBytes(entry);
}
catch(IOException e)
{
showStatus("Cannot write on to the log file"+e);
}
}
}
}
I even set the Policytool such that "Customer.txt" file has both read and write privileges.So any suggestions please? Thanks.
The code is as follows...
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
//<applet code=Login.class Width=400 Height=400>
//</applet>
public class Login extends JApplet implements ActionListener
{
JTextField textcustname;
JPasswordField textpassword;
JLabel labelcustname;
JLabel labelpassword;
JButton loginbutton;
public void init()
{
createApplet();
}
public void createApplet()
{
Container content;
content=getContentPane();
content.setLayout(new FlowLayout());
labelcustname=new JLabel("Customer Login Name:");
textcustname=new JTextField(10);
labelpassword=new JLabel("Password:");
textpassword=new JPasswordField(10);
loginbutton=new JButton("Login");
content.add(labelcustname)
content.add(textcustname);
content.add(labelpassword)
content.add(textpassword);
content.add(loginbutton);
loginbutton.addActionListe
}
public void actionPerformed(ActionEven
{
Object obj=evt.getSource();
if(obj==loginbutton)
{
String entry=textcustname.getText
try
{
RandomAccessFile logfile=new RandomAccessFile("D:\\java
logfile.seek(logfile.lengt
logfile.writeBytes(entry);
}
catch(IOException e)
{
showStatus("Cannot write on to the log file"+e);
}
}
}
}
I even set the Policytool such that "Customer.txt" file has both read and write privileges.So any suggestions please? Thanks.
Please post errors
ASKER
But I cant see all the errors since one cant scroll up in the DOS window.So any way to look through all the errors or can I post whichever is visible?
You'll have to get your DOS window organized. You have the following potential problems/potential problems:
1. You don't close the file
2. You don't check for presence of both username and password
3. Your policy file may not have been altered properly
1. You don't close the file
2. You don't check for presence of both username and password
3. Your policy file may not have been altered properly
ASKER
at java.awt.Component.process MouseEvent (Component .java:5100 )
at java.awt.Component.process Event(Comp onent.java :4897)
at java.awt.Container.process Event(Cont ainer.java :1569)
at java.awt.Component.dispatc hEventImpl (Component .java:3615 )
at java.awt.Container.dispatc hEventImpl (Container .java:1627 )
at java.awt.Component.dispatc hEvent(Com ponent.jav a:3477)
at java.awt.LightweightDispat cher.retar getMouseEv ent(Contai ner.java:3 483
)
at java.awt.LightweightDispat cher.proce ssMouseEve nt(Contain er.java:31 98)
at java.awt.LightweightDispat cher.dispa tchEvent(C ontainer.j ava:3128)
at java.awt.Container.dispatc hEventImpl (Container .java:1613 )
at java.awt.Component.dispatc hEvent(Com ponent.jav a:3477)
at java.awt.EventQueue.dispat chEvent(Ev entQueue.j ava:456)
at java.awt.EventDispatchThre ad.pumpOne EventForHi erarchy(Ev entDispatc hTh
read.java:201)
at java.awt.EventDispatchThre ad.pumpEve ntsForHier archy(Even tDispatchT hre
ad.java:151)
at java.awt.EventDispatchThre ad.pumpEve nts(EventD ispatchThr ead.java:1 45)
at java.awt.EventDispatchThre ad.pumpEve nts(EventD ispatchThr ead.java:1 37)
at java.awt.EventDispatchThre ad.run(Eve ntDispatch Thread.jav a:100)
These are the ones I am able to see.
at java.awt.Component.process
at java.awt.Container.process
at java.awt.Component.dispatc
at java.awt.Container.dispatc
at java.awt.Component.dispatc
at java.awt.LightweightDispat
)
at java.awt.LightweightDispat
at java.awt.LightweightDispat
at java.awt.Container.dispatc
at java.awt.Component.dispatc
at java.awt.EventQueue.dispat
at java.awt.EventDispatchThre
read.java:201)
at java.awt.EventDispatchThre
ad.java:151)
at java.awt.EventDispatchThre
at java.awt.EventDispatchThre
at java.awt.EventDispatchThre
These are the ones I am able to see.
That's of no use unfortunately - it simply tells you that the problem's occurring in the event handler - which is pretty obvious anyway. You need to get your window organized and post the whole stack trace. I have no difficulty scrolling my DOS window - set its properties by right-clicking the title bar. Use the onboard help if you have difficulty
ASKER
Hmm i tried a lot but in vain :( I'am using win98 so maybe its not possible to view in it i guess :(
Anyway I asked about it in "Platforms/Win progs" section so lets see if anyone replies.
Anyway I asked about it in "Platforms/Win progs" section so lets see if anyone replies.
ASKER
But when I open Policytool.exe it says Error Could not find policy File C:\WINDOWS\.java.policy
But my policy file is in D:\java\bin
But another program didnt had any problems in displaying a .gif image in applet.I mean same error occured but I granted Read and Execute to all files and I was able to display that image.
But my policy file is in D:\java\bin
But another program didnt had any problems in displaying a .gif image in applet.I mean same error occured but I granted Read and Execute to all files and I was able to display that image.
Writing is quite different - go through the process again carefully - that is where your problem lies
ASKER
Hmm but Can you try my above code and check if you are getting any errors in it or able to successfully write to a file? So that if there are no errors in that code then maybe Policyfile settings should be wrong.And what did you meant by carefully go through the process.I mean which process?
The only errors I get are security ones, although, as I mentioned before, you don't close your file, so even if there were no security errors, it wouldn't write to the file. Close it in a finally block.
>>I mean which process?
The process of doing the security settings
>>I mean which process?
The process of doing the security settings
ASKER
Sorry how to close a file?
RandomAccessFile logfile = null;
try
{
logfile = new RandomAccessFile("D:\\java \\bin\\Cus tomer.txt" ,"rw");
logfile.seek(logfile.lengt h());
logfile.writeBytes(entry);
}
catch(IOException e)
{
showStatus("Cannot write on to the log file"+e);
}
finally
{
try { logfile.close(); } catch(IOException e) { /* ignore */ }
}
try
{
logfile = new RandomAccessFile("D:\\java
logfile.seek(logfile.lengt
logfile.writeBytes(entry);
}
catch(IOException e)
{
showStatus("Cannot write on to the log file"+e);
}
finally
{
try { logfile.close(); } catch(IOException e) { /* ignore */ }
}
ASKER
I did but still same errors are occuring :(
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
So I have my Login.java in the directory "D:\java\bin"
So is the following code correct for .java.policy file?
grant codeBase "file:/D:/java/bin/-"
{
permission java.io.FilePermission "customer.txt", "read";
permission java.io.FilePermission "customer.txt", "write";
};
And what do you mean by "You shouldn't have data/code in your Java bin " ?
So is the following code correct for .java.policy file?
grant codeBase "file:/D:/java/bin/-"
{
permission java.io.FilePermission "customer.txt", "read";
permission java.io.FilePermission "customer.txt", "write";
};
And what do you mean by "You shouldn't have data/code in your Java bin " ?
>>And what do you mean by "You shouldn't have data/code in your Java bin " ?
You shouldn't put data and code in there.
The code is right if customer.txt is in the same directory
You shouldn't put data and code in there.
The code is right if customer.txt is in the same directory
ASKER
Well I also tried with "Customer.txt" in the directory
C:\Program Files\Java\j2re1.4.2_01\bi n
and creating .java.policy file in "D:\java\bin" as follows
grant codeBase "file:/C:/Program Files/java/j2re1.4.2_01/bi n/-"
{
permission java.io.FilePermission "customer.txt", "read";
permission java.io.FilePermission "customer.txt", "write";
};
Then started appletviewer from "D:\java\bin" as follows
appletviewer -J-Djava.security.policy=. /.java.pol icy Login.java
But still the same problem.
C:\Program Files\Java\j2re1.4.2_01\bi
and creating .java.policy file in "D:\java\bin" as follows
grant codeBase "file:/C:/Program Files/java/j2re1.4.2_01/bi
{
permission java.io.FilePermission "customer.txt", "read";
permission java.io.FilePermission "customer.txt", "write";
};
Then started appletviewer from "D:\java\bin" as follows
appletviewer -J-Djava.security.policy=.
But still the same problem.
Change your catch so you can see the error:
catch(Exception e)
{
showStatus("Cannot write on to the log file"+e);
}
catch(Exception e)
{
showStatus("Cannot write on to the log file"+e);
}
ASKER
Yes in the status bar of the applet the following error is being displayed--
Cannot write on to the log filejava.security.AccessCo ntrolExcep tion: access denied (java.io.FilePermission D:\Java\bin\Customer.txt write)
So what to do?
Cannot write on to the log filejava.security.AccessCo
So what to do?
try:
grant codeBase "file:/D:/java/bin/-"
{
permission java.io.FilePermission "Customer.txt", "read";
permission java.io.FilePermission "Customer.txt", "write";
};
grant codeBase "file:/D:/java/bin/-"
{
permission java.io.FilePermission "Customer.txt", "read";
permission java.io.FilePermission "Customer.txt", "write";
};
ASKER
Ah Thanks now I'am able to.Actually the problem was I did "customer.txt" instead of "Customer.txt" in that grant codeBase as CEHJ did above :) Thanks objects also.
>>Actually the problem was I did "customer.txt" instead of "Customer.txt" in that grant codeBase as CEHJ did above
It doesn't actually make any difference on Windows (certainly on Win2K)
It doesn't actually make any difference on Windows (certainly on Win2K)
ASKER
Yes it CEHJ ! That was the problem actually.When I changed that it started working :)
Worked for me either way. Maybe the file had been cached in some way
Just checked. My file is called customer.txt
permission java.io.FilePermission "Customer.txt", "read";
permission java.io.FilePermission "Customer.txt", "write";
and
permission java.io.FilePermission "customer.txt", "read";
permission java.io.FilePermission "customer.txt", "write";
work equally well.
permission java.io.FilePermission "Customer.txt", "read";
permission java.io.FilePermission "Customer.txt", "write";
and
permission java.io.FilePermission "customer.txt", "read";
permission java.io.FilePermission "customer.txt", "write";
work equally well.
ASKER
ok :)
> Actually the problem was I did "customer.txt" instead of "Customer.txt"
No the problemn was with the specification of directory.
No the problemn was with the specification of directory.