CrazyEngineers
  • Doubt in java programming....?

    Updated: Oct 14, 2024
    Views: 966
    check this codes guy's...
    import java.io.*;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    /*<Applet code="p" height=400 width=400></applet>*/
    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.
    0
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • Morningdot Hablu

    MemberDec 9, 2010

    Help me guy's.
    Don't know why exception is occurred.
    Not able to execute the try block of RandomAccessFile.
    Are you sure? This action cannot be undone.
    Cancel
  • Manish Goyal

    MemberDec 9, 2010

    initialize string variable with an empty string
    Are you sure? This action cannot be undone.
    Cancel
  • Morningdot Hablu

    MemberDec 9, 2010

    No it's not working...!!
    Are you sure? This action cannot be undone.
    Cancel
  • HirenBarbhaya

    MemberDec 10, 2010

    what is the exception that you are getting??

    Check the help for RandomAccessFile class...
    #-Link-Snipped-#
    Are you sure? This action cannot be undone.
    Cancel
  • Morningdot Hablu

    MemberDec 10, 2010

    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.
    Are you sure? This action cannot be undone.
    Cancel
  • HirenBarbhaya

    MemberDec 10, 2010

    It would be good.. if you can tell us kind of exception that you are getting....
    Are you sure? This action cannot be undone.
    Cancel
  • Manish Goyal

    MemberDec 10, 2010

    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
    Are you sure? This action cannot be undone.
    Cancel
  • Morningdot Hablu

    MemberDec 10, 2010

    Yeh it's workig...thanks goyal !!
    Are you sure? This action cannot be undone.
    Cancel
  • HirenBarbhaya

    MemberDec 10, 2010

    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..
    Are you sure? This action cannot be undone.
    Cancel
  • Andrew Thompson

    MemberDec 10, 2010

    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? ;-)
    Are you sure? This action cannot be undone.
    Cancel
  • Andrew Thompson

    MemberDec 11, 2010

    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.
    Are you sure? This action cannot be undone.
    Cancel
  • Manish Goyal

    MemberDec 11, 2010

    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
    Are you sure? This action cannot be undone.
    Cancel
  • Andrew Thompson

    MemberDec 11, 2010

    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.
    Are you sure? This action cannot be undone.
    Cancel
  • csiscool

    MemberDec 11, 2010

    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-#
    Are you sure? This action cannot be undone.
    Cancel
  • Andrew Thompson

    MemberDec 11, 2010

    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.
    Are you sure? This action cannot be undone.
    Cancel
  • Morningdot Hablu

    MemberDec 11, 2010

    Wow nice information..!!
    .
    Thanks andrew.
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register