The important code:

Browser browser = new Browser(shell, SWT.NONE);
	URL url = this.getClass().getResource("/example.html");
	browser.setUrl(url.toString());
	shell.open();

P.S. I've carefully considered what Masijade and others told me in the other thread; the example.html file is located in multiple locations, including in the same directory as the .class file. . I also tried url.getFile(), and I also tried "example.html", among a lot of other attempts, none worked. I got the html page to display once or twice with getResource(), and it always works if I specify the exact path, but I cannot do that in the real product. If anyone sees any errors please let me know. I also looked at the example on the swt website at eclipse.org, but their example forces the user to specify the pathname (by browsing their machine) so it is fundamentally different than mine.


The full code:

final Shell shell = new Shell(display);
	shell.setText("Basic Tutorial");
	shell.setLayout(new GridLayout());
	shell.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
	Browser browser = new Browser(shell, SWT.NONE);
	browser.setLayout(new GridLayout());
	browser.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
	URL url = this.getClass().getResource("/example.html");
	browser.setUrl(url.toString());
	shell.open();

	while (!shell.isDisposed()) {
		if (!display.readAndDispatch()) display.sleep();
	}
	shell.dispose();

Recommended Answers

All 4 Replies

Is example.html inside jar file?

Nope. And even more strangely, if I put that code that I showed you guys inside the main method - it works. But if I put it inside the constructor - it doesn't work. (And I can't run it from the main method since it isn't going to be the main app).

All code inside constructor
"example.html" is ,where source code is.

//..
        browser.setBounds(5, 75, 800, 600);
        java.net.URL outputURL = getClass().getProtectionDomain().getCodeSource().getLocation();
        String outputString = outputURL.toString();
        System.out.println("outputURL=" + outputString + "example.html");
        String it = outputString + "example.html";
        System.out.println(it);
        browser.setUrl(it);
//..

notice, this not work from jar file

BestJewSinceJC>Nope. And even more strangely, if I put that code that I showed you guys inside the main method - it works. But if I put it inside the constructor - it doesn't work. .
I skip SWT statements and test your code. It's working.

package test;
import java.io.*;
import java.net.*;

public class p1{
   public p1()  throws Exception {
        URL url = this.getClass().getResource("/example.html");
	System.out.println(url.toString());
        System.out.println(url.getFile());
   }
   public static void main(String []args) throws Exception {
          new p1(); 
   }
}

P.S. Path of example.html - for getResource("/example.html") - place example.html in parent of test package.
for getResource("example.html") - place example.html inside test (package) folder.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.