#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    9
    Rep Power
    0

    What does a package statement do?


    I have class1 which calls class2.

    The result is supposed to load and display some images.

    When I ran class1 from Eclipse, it displayed the window but not the images. Debugging seemed to show that the images had been loaded (although I haven't got my head around all the info the debugger gives yet, so can't be sure.)

    When I compiled and ran from the command line, I got the error
    Code:
    33: error: cannot find symbol
    pointing to class2, even though class2.class exists in that folder.

    When I removed the package statement from line 1 of the two files, they worked from the command line, but not in Eclipse. Eclipse told me I needed that line.

    What is going on? I thought a package statement was essentially a bit like an extra search path. If the resource files are actually in the same directory as the class files, I don't understand how the package statement (which was to that same directory) could mess things up.

    I feel I might cope with the basics of algorithms and looping etc, but I can't even get to that because this other stuff about class paths and packages and the same script working differently in different places keeps knotting itself around me like a planet of tangled string.

    Any help please?
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    9
    Rep Power
    0
    I've got my programme to work from the command line. I am just running the script from its own directory.

    However, Eclipse still reports errors. Quickfix suggests either moving the files to the default package or (again) adding the package statement. Neither work.

    If I run the file as it is from Eclipse, I get a window with no images. The first error reported is
    The declared package "" does not match the expected package "JavaLessons".
    If I copy the files (including images) to the default package, I get no errors but the blank window again appears. (They run properly from the command line from this directory.)

    If I do not copy the files but let Eclipse add the package statement to both the java files, I again get no errors but the blank window appears. (They do not run from the command line.)

    Can any one please make sense of this?
  4. #3
  5. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,714
    Rep Power
    348
    When a class is in a package, the package name becomes part of the classname.
    To execute the class: abc in the package: def the java command would be:
    java def.abc
    The directory where the java command is issued needs to be in a folder on the classpath.
    The .class file must be in the def folder.
    If the default classpath is the current folder where the java command is issued, the def folder must be in the current folder.

    For testing: Write a simple HelloWorld program that prints a message and try it with and without a package statement.
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    9
    Rep Power
    0
    Thank you very much, the mist is starting to clear. I understand how this is working in the command line now.

    I have a question about Eclipse. It's clear Eclipse isn't finding the image files. But I read that resource files will be found by the class that calls them if they are in the same folder as the class file. But they are in the same folder. Why isn't Eclipse finding them? (n.b. the programme works fine from the command line now.)

    Thanks again, though. At least I feel I've got the command line.
  8. #5
  9. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,714
    Rep Power
    348
    To see where the program is looking for the image files, add a File class object that uses one of the image files, and print out the absolute path for the File object.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    9
    Rep Power
    0
    Thank you very much again. I haven't learnt anything about File objects yet, but it'll be a useful exercise. May take me a while!
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    9
    Rep Power
    0
    Took me less time than I thought!

    In Eclipse, getAbsolutePath gives the correct path of the image file that is in the same folder as the class, which makes sense.

    But again, the window is not showing the images, which doesn't.

    There's nothing essential wrong with the programme, because it works from the command line. And Eclipse is finding the images. So why might they be not showing up?
  14. #8
  15. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,714
    Rep Power
    348
    And Eclipse is finding the images. So why might they be not showing up?
    If the images are found, but not seen, can there be anything else different?

    I have no idea why if the image files are found for both ways of executing the program why they would not be shown in both cases.

    Can you make a small program that compiles, executes and shows the problem and paste it here?

    Are you reading the image files as files or as resources?
    The getResource() method looks on the classpath.
    Last edited by NormR; June 27th, 2013 at 04:54 PM.
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    9
    Rep Power
    0
    Sorry, took me a while to do this.

    Like I say, this runs fine from the command line. Eclipse seems to require a package statement. (It is running in a package called JavaLessons.)

    Note, in this version the size of the window is taken from the image size and, when running in Eclipse, it produces a window of no size. But the file statement is returning the correct path to the image.

    Here's the code:
    Code:
    import java.awt.AWTEvent;
    import java.awt.Canvas;
    import java.awt.Color;
    import java.awt.Frame;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.event.WindowEvent;
    import java.io.File;
    
    
    public class ImageWindow extends Frame {
    	public static void main(String arg[]){
    		new ImageWindow();
    		File file1 = new File("test.png");
    		System.out.println(file1.getAbsolutePath());
    	}
    	ImageWindow (){
            ImgWindow iw = new ImgWindow();
            enableEvents(AWTEvent.WINDOW_EVENT_MASK);
            add(iw);
            pack();
            setVisible(true);
        }
        public void processWindowEvent(WindowEvent event) {
            if(event.getID() == WindowEvent.WINDOW_CLOSING)
                System.exit(0);
        }
        public class ImgWindow extends Canvas {
        	Image testimage;
        	ImgWindow () {
        		ImageLoader il = new ImageLoader();
    	        testimage = il.loadImageFromFile("test.png");
    	        il.waitForImage(testimage);
    	        int height = testimage.getHeight(this);
    	        int width = testimage.getWidth(this);
    	        setSize(width,height);
        	}
        	public void paint (Graphics g){
    	        g.drawImage(testimage, 0, 0, this);
        	}
        }
    }
    This is the code for the ImageLoader class, which is just lifted from the tutorial:

    Code:
    import java.awt.Component;
    import java.awt.Toolkit;
    import java.awt.Image;
    import java.awt.MediaTracker;
    import java.net.URL;
    
    public class ImageLoader extends Component {
        public Image loadImageFromFile(String filename) {
            Image image = null;
            Toolkit tk = Toolkit.getDefaultToolkit();
            image = tk.getImage(filename);
            waitForImage(image);
            return(image);
        }
        public void waitForImage(Image image) {
            MediaTracker mt = new MediaTracker(this);
            mt.addImage(image,1);
            try {
                mt.waitForAll();
            } catch(Exception e) {
                e.printStackTrace();
                System.exit(0);
            }
        }
        public Image loadImageFromInternet(String urlString) {
            Image image = null;
            URL url = null;
            try {
                url = new URL(urlString);
            } catch(Exception e) {
                e.printStackTrace();
                System.exit(0);
            }
            Toolkit tk = Toolkit.getDefaultToolkit();
            image = tk.getImage(url);
            waitForImage(image);
            return(image);
        }
    }
  18. #10
  19. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,714
    Rep Power
    348
    Look at using the ImageIO class to read the images instead of your own class.

    Also try using the Swing classes instead of the AWT classes: Frame and Canvas
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    9
    Rep Power
    0
    I will, thanks, but it'll take me a while. I really appreciate your input.

IMN logo majestic logo threadwatch logo seochat tools logo