Doubt in java programming....?

check this codes guy's...
import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/**/
public class p extends JApplet implements ActionListener
{
JLabel l1,l2;
JButton b;
JTextField f1,f2;
String str;
public void init()
{
l1=new JLabel("username");
l2=new JLabel("password");
b=new JButton("submit");
f1=new JTextField(20);
f2=new JTextField(20);
FlowLayout f=new FlowLayout(100,20,25);
Container c=getContentPane();
c.setLayout(f);
b.addActionListener(this);
c.add(l1);
c.add(f1);
c.add(l2);
c.add(f2);
c.add(b);
}
public void actionPerformed(ActionEvent e)
{
Object o=e.getSource();
if(o==b)
{
repaint();
str=f1.getText()+" "+f2.getText();
RandomAccessFile r=null;
try
{
//String dir="mohit";
r=new RandomAccessFile("mo.txt","rw");
r.writeBytes(str);
r.close();
}
catch(Exception ee)
{ }
//r.close();
}
}
public void paint(Graphics g)
{
g.drawString(str,100,200);
}
}
Everything works fine while compiling and executing but unable to create the output text file.
Can any one help me to find the mistakes.

Replies

  • Morningdot Hablu
    Morningdot Hablu
    Help me guy's.
    Don't know why exception is occurred.
    Not able to execute the try block of RandomAccessFile.
  • Manish Goyal
    Manish Goyal
    initialize string variable with an empty string
  • Morningdot Hablu
    Morningdot Hablu
    No it's not working...!!
  • HirenBarbhaya
    HirenBarbhaya
    what is the exception that you are getting??

    Check the help for RandomAccessFile class...
    #-Link-Snipped-#
  • Morningdot Hablu
    Morningdot Hablu
    I know well about RandomAccessFile .....the problem is that i am not able to execute the try block.
    Always an exception is caught by your catch.
    .
    I tried this RandomAccessFile statement separately it works fine in other programs but don't know why in this program particularly an exception is caught.
  • HirenBarbhaya
    HirenBarbhaya
    It would be good.. if you can tell us kind of exception that you are getting....
  • Manish Goyal
    Manish Goyal
    The exception you are getting is

    access denied(java.io.FilePermission mo.txt write)

    In order to remove that you have to provide give some explicit file permissions through a policy file ,you can create it manually or by using policy tool .After that you have to run your applet along with this policy file
  • Morningdot Hablu
    Morningdot Hablu
    Yeh it's workig...thanks goyal !!
  • HirenBarbhaya
    HirenBarbhaya
    Good to see that finally the issue is resolved... I m not Java professional (actually I m .Net professional).. but was interested to know the excetion and then try and find out how we can get rid of it.. but Goyal had solved it..
  • Andrew Thompson
    Andrew Thompson
    goyal420
    The exception you are getting is

    access denied(java.io.FilePermission mo.txt write)

    In order to remove that you have to provide give some explicit file permissions through a policy file..
    That is incorrect. In order to work, the privileges of the applet must be increased.

    Providing a policy file is one way* to do that, but it is a rather silly way. If the applet cannot write a File, it also cannot insert or create a policy file. It would require every user of the applet to put altered policy files into their system. Since policy files are hard enough for programmers to get right, it stands to reason they are a minefield of security risk for end users.

    In the event that the app. is only used by a few users, make it an application (a frame instead of an applet) and provide the end user with a runnable Jar.

    If the applet is intended to be used by many people it would be better to digitally sign the code (* the other way to break out of the sandbox), and induce the user to accept the digitally signed code when prompted.

    And to the OP. Programmers generally hold off answering engineering questions, and I feel engineers should hold off answering programming questions (this thread being a good example of why). I recommend you ask future programming questions on either stackoverflow, or if they relate to Java, the Oracle forums.

    There now. Have I offended everyone in this thread? ;-)
  • Andrew Thompson
    Andrew Thompson
    Oops! Forgot to mention that if the users can be expected to be running a Plug-In 2 architecture JRE (1.6.0_10+), then neither policy files nor digital signing is necessary.

    The Plug-In 2 architecture allows embedded applets to hook into the JNLP API services of Java webstart. These services make it possible for even fully sand-boxed code to read from & write to files on the end user's local file system.

    I have a #-Link-Snipped-# class.
  • Manish Goyal
    Manish Goyal
    Hey thanks for information
    If the applet is intended to be used by many people it would be better to digitally sign the code (* the other way to break out of the sandbox), and induce the user to accept the digitally signed code when prompted.

    Please tell me about this more
  • Andrew Thompson
    Andrew Thompson
    goyal420
    Hey thanks for information
    You are welcome. :smile:

    goyal420
    ..Please tell me about this more
    Code signing?

    I am no expert on how the digital signatures are produced - I just use the code signing tools supplied by Sun/Oracle for the task, usually via a build tool such as Ant.

    Code signing is supposed to be used with a certificate from Verisign or similar that assures the end user that the code is coming from a source that has been verified. However code signing certificates are horrendously expensive so I have always used a self generated certificate. People have argued that these are pointless at protecting the end user, but they get the job done so long as the user trusts you and will accept them.

    Because of the unverified nature of self generated certificates, the warning dialog that pops up for the end user notes that the code comes from an untrusted/unverified source, and the 'always trust' checkbox on the dialog defaults to false.

    You can see the effect of a self-signed certificate at (let me see, ..oh yeah) my #-Link-Snipped-#

    Feel free to refuse the permission. The point is simply to show the trust dialog.

    As far as performing code signing, it is relatively easy using Ant*. Further up the page from the random access file example I linked earlier, is a #-Link-Snipped-# that provides both a completely sand-boxed version, as well as one that requests extended permissions. The sand-boxed demo. does not need to be signed, but the extended permissions version does - so I digitally sign the code used for both.

    Beside the demo. links, there is a downloadable zip archive containing the complete source and a build file (* Ant style build.xml) that will (generate a certificate &) sign the code.
  • csiscool
    csiscool
    Andrew Thompson
    That is incorrect. In order to work, the privileges of the applet must be increased.

    Providing a policy file is one way* to do that, but it is a rather silly way. If the applet cannot write a File, it also cannot insert or create a policy file. It would require every user of the applet to put altered policy files into their system. Since policy files are hard enough for programmers to get right, it stands to reason they are a minefield of security risk for end users.

    In the event that the app. is only used by a few users, make it an application (a frame instead of an applet) and provide the end user with a runnable Jar.

    If the applet is intended to be used by many people it would be better to digitally sign the code (* the other way to break out of the sandbox), and induce the user to accept the digitally signed code when prompted.

    And to the OP. Programmers generally hold off answering engineering questions, and I feel engineers should hold off answering programming questions (this thread being a good example of why). I recommend you ask future programming questions on either stackoverflow, or if they relate to Java, the Oracle forums.

    There now. Have I offended everyone in this thread? ;-)
    I like your boldness 😉
    Learned something new today 😀
    On behalf of CEans, I welcome you to CE.
    @tandrew-thompson:- Can you introduce about yourself in #-Link-Snipped-#
  • Andrew Thompson
    Andrew Thompson
    csiscool
    I like your boldness 😉
    ..
    Thanks! :smile:

    csiscool
    ..
    Learned something new today :smile:
    ..
    You are welcome.

    I definitely like online forums. If there was a day goes by contributing to forums that I do not learn something new, I cannot remember it. ;-)

    csiscool
    ..
    On behalf of CEans, I welcome you to CE.
    @tandrew-thompson:- Can you introduce about yourself in #-Link-Snipped-#
    Thanks also for the welcome, but..

    What is this, a dating site?!? I have chosen not to do the intro. template*, but filled in the #-Link-Snipped-# with some extra information.

    * After years of stressing to people on technical forums that I am not their buddy - it would seem a little hypocritical now to begin doing the same types of things that prompted the remark. I hope you will forgive my small aberration from the accepted way this forum operates.
  • Morningdot Hablu
    Morningdot Hablu
    Wow nice information..!!
    .
    Thanks andrew.

You are reading an archived discussion.

Related Posts

You are given 9,9,9,9,5,5,5,5,3,3,3,3,1,1,1,1.Now you are supposed to select exactly 6 no.s from the given no.s so that the sum is exactly 21. It's a challenge. If you are a...
The wordpress 3.0.3 update has been released on WordPress.org and it fixes a security issue related to sites with remote publishing enabled. Remote publishing is disabled by default, but you...
Emblazon′10 is the National Level Student Technical fest organized by the Department of Electronics and Communication Engineering of JNTUH College of Engineering, Nachupally(Kondagattu), Karimnagar Dt. (JNTUHCEJ). This symposium mainly aims...
PIXEL- 2K10- A National Level Technical Symposium for Students of B. Tech, M. Tech & M.C.A to be held on December 15th & 16th, 2010. The Symposium serves as a...
The largest social and cultural college fest in the country Ever since its inception in 1959, it holds the recognition of being the most famous festival in eastern India. Organizing...