Using JDIC to open web browser

Hi, I am trying to figure out JDIC (0.9.1), and how to use it to open an htm file in Internet Explorer.

My issue is that when I compile, it always says that It can't reference non-static method open(Java.io.File) from static context.

Its definitely NOT in a static context.

publicvoid myMethod(){

File testFile =new File("Index.htm");

Desktop.open(testFile);

}

I get the error on Desktop.open

To install it, I pretty much just copied jdic.jar, packager.jar, WinMsiWrapper.dll, and IeEmbed to java's working directory.

Anyone knoe what the issue is? Thanks

[796 byte] By [snoboardera] at [2007-11-27 6:17:10]
# 1

> Hi, I am trying to figure out JDIC (0.9.1), and how

> to use it to open an htm file in Internet Explorer.

> My issue is that when I compile, it always says that

> It can't reference non-static method

> open(Java.io.File) from static context.

>

> Its definitely NOT in a static context.

> > public void myMethod(){

>

>File testFile = new File("Index.htm");

> Desktop.open(testFile);

> }

>

>

That definitely IS a static context.

When you see the following.

ClassName.methodOrProperty

That is a static context because you are invoking a method or accessing a property of the class. Not an instance (object) of the class.

Note the difference between that and this

instanceName.methodOrProperty

So you need a reference to a Desktop instance.

cotton.ma at 2007-7-12 17:29:50 > top of Java-index,Java Essentials,Java Programming...
# 2
huh, I did not know that. def. feel kinda stupid....Thanks
snoboardera at 2007-7-12 17:29:50 > top of Java-index,Java Essentials,Java Programming...
# 3
> huh, I did not know that. def. feel kinda stupid....It's okay, asking questions is how you learn. :)
cotton.ma at 2007-7-12 17:29:50 > top of Java-index,Java Essentials,Java Programming...
# 4

Haha, yeah, I guess I just hate the learning curve...

Ok, one last thing, to verify, this code gives a reference to the Desktop instance, correct?

Desktop desktop = new Desktop();

?

This particular code tells me that Java.awt.Desktop() has private access. Do I have to override it somehow? Thanks again

snoboardera at 2007-7-12 17:29:50 > top of Java-index,Java Essentials,Java Programming...
# 5

> Haha, yeah, I guess I just hate the learning

> curve...

>

> Ok, one last thing, to verify, this code gives a

> reference to the Desktop instance, correct?

>

> Desktop desktop = new Desktop();

>

> ?

>

> This particular code tells me that Java.awt.Desktop()

> has private access. Do I have to override it somehow?

> Thanks again

You have to call a method from the API that gives you a reference that you can actually call (so not private) . I don't know what that might be...

Hold on

cotton.ma at 2007-7-12 17:29:50 > top of Java-index,Java Essentials,Java Programming...
# 6

> This particular code tells me that Java.awt.Desktop() has private access. Do I have

> to override it somehow?

The fact that it has private access makes sense if you stop and think about it - there is only one desktop, and you have to use that desktop. The programmer can't go making their own! (Well, they can - but it won't have the desired effect which is to interact with the real desktop with its real browser etc).

There's no overriding it! What you have to do is go to the API documentation (http://java.sun.com/javase/6/docs/api/java/awt/Desktop.html) and see if you can find a method using which you can get a Desktop.

pbrockway2a at 2007-7-12 17:29:50 > top of Java-index,Java Essentials,Java Programming...
# 7
Wait...That IS a static method.So you do need to call it in a static context.What was the exact error message? And am I looking at the right documentation?https://jdic.dev.java.net/nonav/documentation/javadoc/jdic/org/jdesktop/jdic/desktop/Desktop.html
cotton.ma at 2007-7-12 17:29:50 > top of Java-index,Java Essentials,Java Programming...
# 8
> Wait...> > That IS a static method.no it isn'tDesktop.getDesktop().open(file);
prob.not.sola at 2007-7-12 17:29:50 > top of Java-index,Java Essentials,Java Programming...
# 9
> That IS a static method.> So you do need to call it in a static context.Ha! The OP (or their IDE) made the same mistake I did - and imported the wrong Desktop.
pbrockway2a at 2007-7-12 17:29:50 > top of Java-index,Java Essentials,Java Programming...
# 10
> What was the exact error message? And am I looking at> the right documentation?hmm. my previous comment was based off the guy above you's link.
prob.not.sola at 2007-7-12 17:29:50 > top of Java-index,Java Essentials,Java Programming...
# 11
to cottonyeah that's the right APII get Desktop() has private access in java.awt.Desktopand the first error message has disappearedAnd How the heck would I import the right Desktop then?Message was edited by: snoboarder
snoboardera at 2007-7-12 17:29:50 > top of Java-index,Java Essentials,Java Programming...
# 12

> to cotton

>

> yeah that's the right API

>

> I get Desktop() has private access in

> java.awt.Desktop

>

> and the first error message has disappeared

Holy **** I'm getting confused. (not your fault ).

As pbrockway said your IDE thinks you are using one Desktop but you are trying to use another.

But maybe you do actually want to use java.awt.Desktop and just scrap the JDIC...

cotton.ma at 2007-7-12 17:29:50 > top of Java-index,Java Essentials,Java Programming...
# 13
> > And How the heck would I import the right Desktop> then?If you want to use java.awt.Desktop then you already did. It looks like it has the same methods and such as the other one.
cotton.ma at 2007-7-12 17:29:50 > top of Java-index,Java Essentials,Java Programming...
# 14
aaannnnnddd we have a winner!!!I ended up ditching the JDICfinal code File testFile = new File("Index.htm");Desktop.getDesktop().open(testFile);
snoboardera at 2007-7-12 17:29:50 > top of Java-index,Java Essentials,Java Programming...
# 15
*wave hand*this is not the desktop you are looking for.
prob.not.sola at 2007-7-21 21:50:11 > top of Java-index,Java Essentials,Java Programming...