jdk1.1.6 vs 1.2.2

for 1.1.6, creating a thread wasn't a problem using             t=new Thread(this, "Thread1");
            t.start();

but under 1.2.2, it'll give Accesscontrolexception. How do i solve this?

And for read/writing to file,
            try { // opening a file for reading/writing
                  file = new RandomAccessFile("players.pid","rw");
            }
            catch (IOException e) {
                  showStatus(e.toString());
                  System.exit(1);
            }
Works.

But under 1.2.2, it doens't.

Any help?
java_kevinAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jerchCommented:
Define a policy file which contains:

grant {
permission java.security.AllPermission;
};

and save it as mypolicy

and run your program like this

java -Djava.security.policy=mypolicy YourProgram

or you can add these line in your main() method.

Properties props = System.getProperties();

props.setProperty("java.security.policy", "mypolicy");

System.setProperties(props);

Hope this helps.

Jerson


0
java_kevinAuthor Commented:
It doesn't work.

It says:
Class Properties not found.

for the main() method.
0
java_kevinAuthor Commented:
If I set add into the main() method,  Class Properties will not be found.

If I set the properties during runtime, it will work. ie using -D command line

If I'm using applet? Is there anyway to set it?
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

jerchCommented:
You cannot open a file in an Applet.  This is a security restriction. Only signed applet can read from a local file. Inquire www.verisign.com about signed applet.

But if you want it to work in an application. Simply add this line:

import java.util.*;

since Properties is under java.util

Hope this helps.


Jerson


0
java_kevinAuthor Commented:
Adjusted points to 150
0
java_kevinAuthor Commented:
But I may need to print graphics. which will need applet right?

and can frame open html file?
0
jerchCommented:
You can use Panel if you need to print graphics.  Applet is actually a subclass of Panel. By the way, what are you trying to develop.  Is it an application or applet?

:-)

Jerson
0
java_kevinAuthor Commented:
Oh... ehheh...
So I can just use Panel and Graphics will be laoded also. So to create a new Graphics (as using paint in applet, Graphics is auto-created), i will GRaphics g = new Graphics(this) right? Or how?

I'm currently trying to do a game. But I was wondering if I shld use frame or applet, as I was taught applet. Frames sounds pretty unfamiliar. Using applet will allow me to drawstring anytime i want, to display whatever info needed. And though that applet is easier to open a html file in browser.

For frame, it's just that I can't draw anything that i may need to. So, in fact, I'm prepared to switch anytime to either. :)

But since applets can't read files (even if it's calling frame to read), I think I'll need to use frames.

Thanks for being so helpful.
0
jerchCommented:
Try to look at this code. Panel will be the place where you will draw.  It just like an applet. :-) Good luck.

import java.awt.*;
class MyPanel extends Panel {
    public MyPanel () {
        super();
        setSize(new Dimension(400, 400));
    }
   
    public void paint(Graphics g) {
        g.drawString("Hello World", 20, 20);
        g.drawRect(100, 100, 50, 50);
    }
}


class MyApp extends Frame {
    public MyApp() {

        MyPanel p = new MyPanel();
        setBounds(0, 0, 400, 400);      
        add(p);

        setVisible(true);
    }
   
    public static void main(String[] arg) {
        MyApp m = new MyApp();
    }
}


Jerson
0
java_kevinAuthor Commented:
again "hello world"?? haah!!

But then, if something need to be drawn on panel, i dun think i can pass any parameters to the panel class right? I tried to pass from frame class to applet class (int and string) but encountered nullpointerexception.

Real thanks.
0
java_kevinAuthor Commented:
Hi again...
The paint seems to come out rather late. When is the paint invoked? in ur eg.
0
jerchCommented:
Yes you can simply pass the frame object to the MyPanel by using this.


Follow the following steps:
1. add an attribute to your MyPanel:
Frame myFrame;

2. change the contructor that accepts a Frame

MyPanel(Frame f) {
....
myFrame = f;
....
}

3. change the way you instantiate MyPanel

new MyPanel(this);

4. add some methods in your my MyApp that returns data from the frame. For example, add this method in your MyApp:

public String getData() {
return "This data is from the frame";
}

5. Lastly, in the paint() method of MyPanel.  You can access the data of MyApp by calling:
myFrame.getData()

so it will look like this.
g.drawString(myFrame.getData(), 40, 40);

To learn more about AWT visit http://java.sun.com/docs/books/tutorial/

There's a downloadable tutorial on AWT or you can actually download the whole tutorial on Java. ;-)


Jerson









0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
java_kevinAuthor Commented:
Adjusted points to 200
0
java_kevinAuthor Commented:
But why does paint() get invoked rather slowly?

I mean, after myPanel came out, it took abt 1 second or so to paint the line. What caused the delay? Although it doesn't matter much :)

*Accepted ur answer also. Obsessed with going into details that I forgot to accept it*
0
jerchCommented:
No that's okay.

I think that's the limitation of Java. It's not as fast as native code.  For my computer, it's rather fast.  Maybe it has to do with the hardware.

Thanx

Jerson
0
java_kevinAuthor Commented:
Oh yeah, I forgot to mention. As this is a project, and you helped me with these, do u mind giving ur info to me in case i need to acknowledge ur help?

And i believe you're working in the network company where access to net is god-speed.
So do you mind if, when I encounter more problems, I post them straight away here?

Hey, in this case, since using Panel provides graphics, I might as well use frame, but include panels. Frame for reading file and running app :) *dirty trick though* :)

Thansk for the great help :)
0
jerchCommented:
Name: Jerson Chua
Title: Java Application Engineer
Company: CyberJ Resources Inc.
Email: jerson@rocketmail.com


By the way, are you a student or a programmer that is working in an internet company and this is your first project.

Good luck....

Jerson
0
java_kevinAuthor Commented:
Hi,
I'm not sure if you mind me asking another question here instead of posting as a new question.

I noticed that if frame1 calls frame2, and makes itself disabled, when frame2 finishes, frame one loses the focus and goes to bg. Any solution for this? This ain't important, but just to make it kinda user frenly, or does it? :)
0
jerchCommented:
Of course it's better if you post again, but i dont really mind that :-)

I think you can change frame2 into a dialog box so you don't have to disable frame1 by simply making the dialog box modal.  Modal means that the parent frame is disabled while the child dialog box is active.  By the way do you have the API doc with you, it would be of great help if you would refer to it while programming.  

Jerson
0
java_kevinAuthor Commented:
API doc? I'm not very sure about that. But I did download the winhelp from javasoft. It provided some help in writing the codes.

As for the modal frame, I used it for the msgboxes. But when someone is viewing a frame, and clicks a button which causes a new frame to come up, I somehow need to make the new frame modal. But that's not important though...  I can do something to check that.

And yes, I'm a student, doing my graduation project.
0
jerchCommented:
Check Dialog in java.awt package. I guess the winhelp it's the same thing with the API doc the difference is that it's in WinHelp format.
0
java_kevinAuthor Commented:
For ICQ, whenever u r sent a URL, it'll be appended to the bookmark file.

For Java, can it be done? ie, output as text only.

I tried DataOutputStream with writeInt/UTF/Chars. Int doesn't appear as number. UTF outputs correctly (no space between letters). Chars had space between them.
0
jerchCommented:
Sorry... I dont get your question?
0
java_kevinAuthor Commented:
sorry, I mean,... in short, can java save data in plain text format? ie nubmers save as nubmers, and special characters ( < ; ! as selves.

So that people can view the stuff in text editors. As I think I need to append some data to html files...
0
jerchCommented:
code:
import java.io.*;

FileOutputStream fos = new FileOutputStream("myfile.txt");

OutputStreamWriter osw = new OutputStreamWriter(fos);

String str = "This is my text with the length of 38.";

osw.write(str, 0, str.length);

fos.close();
osw.close();

You can do this if you're just trying to save some ASCII file but if you want to write UNICODE characters (for internationalization which support different languages.) you should use FileWriter instead.
0
java_kevinAuthor Commented:
thanks.

if i need to append to this file, I'll simply add in true as the 2nd parameter right? in the FOS.

And if you feel that replying and answering here is troublesome, feel free to email optimist@myself.com
0
jerchCommented:
Exactly, just change the

FileOutputStream fos = new FileOutputStream("myfile.txt");

to

FileOutputStream fos = new FileOutputStream("myfile.txt", true);

:-)
0
java_kevinAuthor Commented:
Sorry, yet another question.

I'm using RandomAccessFile to keep track of the players' information. A problem comes when deleting any record.

When the user wants to delete a record, the record in the file will be blank But NOT deleted. meaning, a record with blank data. I use a Choice to let them choose which player's info to view. So after deelting the record, I have problems keeping track of which player the user wants to view as the index will be kinda mixed up.

I'm thinking of:
1) copying the data of existing (database)file to new one so no blank record. Then I'll copy them back again to the old file.

2) Move records, after the deleted one, one position up the file. But will this waste system resources?
0
jerchCommented:
I'm not with clear with 1. In number 2 do you mean that you're going to move each record up to the deleted record? if yes, i think it's ok. you even save space. If you mean system resource as processing time, that's ok since CPU nowadays are powerful.

I hope I got your question right :)

Jerson
0
java_kevinAuthor Commented:
well, as for the first qn, I'm not sure if you mean you are not clear about the question or you are not sure of the answer.

I mean, after someone has deleted any record, the remaining ones will be copied to a new file so that there'll be no blank records. Then the new file's data will be transferred to the original file. (This sounds kinda troublesome though).

My tutor suggested that I can rename the file, which will be kinda troublesome to me.
0
jerchCommented:
Why don't you move all the records after the deleted record by simply
using readFully(byte[] b, int off, int len) and write(byte[] b, int off, int len) and delete the last record (using the setlength) since you will have a double copy of the last record?

Code:
int size = <number of bytes after the deleted record>;

byte[] buffer =  new byte[size];
file.readFully(buffer, <index position after the deleted record>, 0, size);

file.write(buffer, <index position of the deleted record>, size);

file.setLength(<original length of the file minus the record size>);

This is just an idea on how you can do it. Hope it works.

Jerson

0
java_kevinAuthor Commented:
what is "index position after the deleted record"? do u mean the starting position (in bytes) of the record after the deleted record?

And "index position of the deleted record" means similar?

"original length of the file minus the record size" refers to the total length of the "newly-created" file?
0
jerchCommented:
what is "index position after the deleted record"? do u mean the starting position (in bytes) of the record after the deleted record?
-Yes

And "index position of the deleted record" means similar?
-No, this is the starting position of the deleted record

"original length of the file minus the record size" refers to the total length of the "newly-created" file?
-Exactly...
0
java_kevinAuthor Commented:
Actually, by using "similar", I mean "the starting position of the deleted record". Just that I'm too lazy to type out the full line, since a similar meaning has been typed out above. :)

Thanks, I believe it should work :) I'll try now.
0
java_kevinAuthor Commented:
And now it works :) But a bit kinda too smooth... that suddenly got lost on what should i do next :)

Ya, If i open a file but don't close it when my program is done, it only holds on to resources right? There won't be much harm.

And is there a way to call the default browser to open up a html doc?

Lastly, i tried using writeutf and writechars. when using setlength with both methods, utf doesn't seem to keep to that length, but chars did. Why is this so? I mean, i used file.length to get the length. i noticed that utf is variable length. and a strange thing with it: when getting text read to textfield, it's ok. but when putting to choice, there are strange chars. ie ((char) 31) and below
0
jerchCommented:
It's better if you close the file and open it again when the program needs it. Actually it depends but if you're system extensively read from the file, then it's okay. You have to actually strike the balance.  It depends on your design choice.

I think there's a way you can call the default browser but I don't know how.

I don't have any idea on your last question.

Jerson
0
java_kevinAuthor Commented:
There's finally something you are not sure :)

Regarding the byte you recommended, I tried again just now. It gave me IndexOutofboundsexcep. Then I tried passing only the byte[] and it's fine.

I tried giving the offset from even the 0 byte and it still gave the same error. Any idea?

And which country are you from? I did not notice u had a chinese surname until I found your language is kinda strange...
0
jerchCommented:
I'm from the Philippines but I'm actually a Chinese.
Start the offset from zero and for the length, use the size of the byte[] by using the length.

e.g.
byte[] b = new byte[10];
size = b.length;
0
java_kevinAuthor Commented:
FileOutputStream fos ;
OutputStreamWriter osw;
try {
fos = new FileOutputStream("myfile.txt", true);
osw = new OutputStreamWriter(fos);
String str = "This is my text with the length of 38.\nhhh";
osw.write(str, 0, str.length);
fos.close();
//osw.close();

}
catch (FileNotFoundException e) {
}
catch (IOException e) {System.out.println(e.toString());}

If osw.close wasn't commented, i'll get Bad file descip error. But either way, the text is still not output to text file.
0
java_kevinAuthor Commented:
The file is created, but no text is sent to the file for appending.
0
java_kevinAuthor Commented:
Last thing...
How do I delete a file using java? File can delete it, but it will need to be CREATED before it can be deleted.
0
jerchCommented:
To delete a file
File f = new File("hello.txt");
f.delete();

Sorry i gave you the wrong code just inter change the two close statements.

class WriteFile {
    public static void main(String[] args) throws Exception {
        FileOutputStream fos = new FileOutputStream("hello.txt");
        OutputStreamWriter osw = new OutputStreamWriter(fos);
        String text = "this is my text";
        System.out.println("before");
        osw.write(text, 0, text.length());
        osw.close(); <--
        fos.close(); <--
    }
}

0
java_kevinAuthor Commented:
class MyPanel extends Applet {
      Image me[];
      int index=0, current;
      public MyPanel() {
            me=new Image[5];
            for (int i=0;i<me.length;i++) {
                  try {
                        me[i]= getImage(new URL("file:///C|/My Documents/fyp/"),"Kitty27"+i+".jpg");                        
                  }
                  catch (MalformedURLException e) {
                        showStatus(e.toString());
                  }
            }
            setBackground(Color.yellow);
      }


the code for loading images can't work... nullptr exception.
0
jerchCommented:
Sorry.. I'm not into that. :-)
0
java_kevinAuthor Commented:
Then, do you know how to load images with frame?
0
java_kevinAuthor Commented:
wondering if you can reply soon...

For my frame and applet combined app, can i implement keylistener? so tat whenever ppl press a key while my app has focus, i can get tat event and do something...

eg, press 'a' "hey, you pressed a"
press 'b' "hey, b was pressed"
0
jerchCommented:
if you're referring to hotkeys see this site

http://www.javaworld.com/javaworld/javaqa/1999-09/02-qa-hotkeys.html

0
java_kevinAuthor Commented:
for the key thing, i've found the ans.

But I've a query.

I'm having applet in a frame, as you may kniow. When i used keylsitener for the frame, it's fine.

but when i applied to the applet, it cna't. Is it 'cos the applet is kinda done inside the frame, so it can't get the keys?
0
jerchCommented:
I haven't really encountered such problem.  Maybe you can ask someone else here. :-)

0
java_kevinAuthor Commented:
i'm not sure if you are hinting something...

For the keylistener, i cna implement it under 1.1.6

but not wif 1.2.2

I requested focus, added keylistener, had the keypres/relea/typ events.
      public mine() {
            setBackground(Color.green);
            addKeyListener(this);
            requestFocus();
      }
      public void keyPressed(KeyEvent e) {
            System.out.println("r "+e.getKeyChar());
      }
      
      public void keyTyped(KeyEvent e) {
            System.out.println("f "+e.getKeyCode());
      }
      
      public void keyReleased(KeyEvent e) {
            System.out.println("v");
      }
0
java_kevinAuthor Commented:
sorry... this question was posted wrongly...
0
java_kevinAuthor Commented:
Hi,
If newAudioFile is available for the jdk1.2.2 in my house, is it possible that it doesn't work in my college, which uses jdk1.2.2 as well?

'Cos at home, I searched for newaudiofile and there's help topic for it. But when I looked for it in the college, it doesn't seem to exist.
HOWEVER (sorry for the caps, but just for the sake of emphasis), there's no error in compiling the program. Only that during runtime, the audio files can't be heard.

I tried at home for some errors: Audio stuff is being used. Can't access it.
And the files can't play. But there's no error in college.

Any suggestions?
0
jerchCommented:
Don't know that topic really... Anyway, don't hesitate to ask if you have other problems.

Jerson
0
java_kevinAuthor Commented:
How about keylistener?

I've got it in my program. But 'cos I've also got a Choice on it, sometimes the choice will get the focus, and nothing can invoke the keylistener. Otherwise, keylistener will take over, since Choice isn't being focused.

Is there a way to get the keylistener to work even if the choice is focused? Or remove the focus from choice? The keys I'm referring to are Up, Left buttons.
0
jerchCommented:
What do you mean the keyPressed, keyReleased and keyTyped are not called when the focus is in the Choice?

Jerson
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.