Results 1 to 18 of 18
  1. #1
    dan0 is offline Member
    Join Date
    Feb 2009
    Posts
    40
    Rep Power
    0

    Default [SOLVED] Cannot find symbol error

    Can anyone tell me how to resolve the following compile-time error?

    TestUserInterface.java:5: cannot find symbol
    symbol: class UserInterface
    location: class projects.web.TestUserInterface
    UserInterface ui = new UserInterface();
    I have two classes: UserInterface and TestUserInterface. Both are in the same package.

    To compile and run them I use the following cmds respectively:

    Java Code:
    javac -d ..\classes projects\web\TestUserInterface.java
    Java Code:
    java -cp ..\classes projects.web.TestUserInterface
    Following is the code for both classes:

    Java Code:
    package projects.web;
     
    import java.awt.*;
    import javax.swing.*;
     
    public class UserInterface extends JFrame{
    	
    	JPanel menuPanel = new JPanel();
    	JPanel contentPanel = new JPanel();
    	JPanel selectionPanel = new JPanel();
    	
    	JButton save = new JButton("Save");
    	JButton addFiles = new JButton("Add");
    	
    	public UserInterface(){
    	super("File Upload");
    	setSize(500, 500);
    	
    	menuPanel.add(addFiles);
    	selectionPanel.add(save);
    	
    	setLayout(new BorderLayout());
    	add(menuPanel, BorderLayout.NORTH);
    	add(contentPanel, BorderLayout.CENTER);
    	add(selectionPanel, BorderLayout.SOUTH);
    	
    	} // end constructor
        
    } // end UserInterface clas
    Java Code:
    package projects.web;
     
    public class TestUserInterface{
    	public static void main(String[] args){
    		UserInterface ui = new UserInterface();
    	}
    } // end TestUserInterface class
    Last edited by dan0; 03-03-2009 at 05:11 PM. Reason: Error resolved

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    The class code both look fine. I see you using a -cp switch. Do you need also to include "this" directory in the class path? by this I mean to include a period: "."?

  3. #3
    dan0 is offline Member
    Join Date
    Feb 2009
    Posts
    40
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    The class code both look fine. I see you using a -cp switch. Do you need also to include "this" directory in the class path? by this I mean to include a period: "."?
    I have included "." (this directory) in the classpath - i.e. in my environment variable "classpath" I have the following,
    .;C:\Program Files\Java\jdk1.6.0_11\lib\tools.jar
    Could the problem be with the package or how I've setup the directory to hold the compiled files?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Could the problem be with the package or how I've setup the directory to hold the compiled files?
    I'm thinking not. If this were the problem, it wouldn't be able to find the TestUserInterface class at all (I think). I'm at a loss to figure this one out. Sorry.

    Edit: Still, I'd try including the period in your java -cp statement just to see what happens. You've nothing to lose.

  5. #5
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    This
    Java Code:
    javac -d ..\classes projects\web\UserInterface.java
    will work just fine, since it doesn't need anything else to compile. This (assuming the problem is during compilation)
    Java Code:
    javac -d ..\classes projects\web\TestUserInterface.java
    however, may not, as it needs the UserInterface class in order to compile. You should try
    Java Code:
    javac -cp ..\classes -d ..\classes projects\web\TestUserInterface.java
    although the -d may assume that automatically, I don't think it does.

    Also, when using -cp, the CLASSPATH environment variable is ignored, and when using -jar, both -cp and the CLASSPATH environment variable is ignored (it will use only the MANIFEST file).

  6. #6
    dan0 is offline Member
    Join Date
    Feb 2009
    Posts
    40
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    I'm thinking not. If this were the problem, it wouldn't be able to find the TestUserInterface class at all (I think). I'm at a loss to figure this one out. Sorry.

    Edit: Still, I'd try including the period in your java -cp statement just to see what happens. You've nothing to lose.
    In the cmd prompt I selected the directory that holds the class file:

    C:\Document and Settings\Dan\My Documents\workspace\classes>
    Then I entered the following cmd:
    Java Code:
    java -cp .; projects\web\TestUserInterface
    I then got the following error:
    Exception in thread "main" java.lang.NoClassDefFoundError: projects\web\TestUserInterface
    NOTE: When I tried the following version of the cmd, while in the directory that has the class files, nothing happened.
    Java Code:
    java -cp .; projects.web.TestUserInterface
    I also tried re-compling the dependent file via:
    Java Code:
    javac -cp ..\classes -d ..\classes projects\web\TestUserInterface.java
    But when I tried to run the class file, I got the same error:
    Exception in thread "main" java.lang.NoClassDefFoundError: projects\web\TestUserInterface
    At this point, I'm thoroughly confused and only managed to give myself a headache...
    Last edited by dan0; 02-28-2009 at 08:45 PM.

  7. #7
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    8

    Default

    Quote Originally Posted by dan0 View Post
    In the cmd prompt I selected the directory that holds the class file:



    Then I entered the following cmd:
    Java Code:
    java -cp .; projects\web\TestUserInterface
    I then got the following error:


    NOTE: When I tried the following version of the cmd, while in the directory that has the class files, nothing happened.
    Java Code:
    java -cp .; projects.web.TestUserInterface
    Which means you were successful, because nothing in that code actually displays the interface, just creates it.

    I also tried re-compling the dependent file via:
    Java Code:
    javac -cp ..\classes -d ..\classes projects\web\TestUserInterface.java
    But when I tried to run the class file, I got the same error:


    At this point, I'm thoroughly confused and only managed to give myself a headache...

  8. #8
    dan0 is offline Member
    Join Date
    Feb 2009
    Posts
    40
    Rep Power
    0

    Default

    Quote Originally Posted by masijade View Post
    Which means you were successful, because nothing in that code actually displays the interface, just creates it.
    That was the problem; I was creating the interface, but not setting it to visible. By adding
    Java Code:
    setVisible(true)
    the interface displayed.

    Thanks for the help; I appreciate it.

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Don't forget to pack it either. Your test class should look something like so:
    Java Code:
    import javax.swing.JFrame;
    import javax.swing.SwingUtilities;
    
    public class TestUserInterface
    {
      private static void createGui()
      {
        UserInterface ui = new UserInterface();
        ui.pack();
        ui.setLocationRelativeTo(null);
        ui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        ui.setVisible(true);    
      }
      
      public static void main(String[] args)
      {
        SwingUtilities.invokeLater(new Runnable()
        {
          public void run()
          {
            createGui();
          }
        });
      }
    }

  10. #10
    dan0 is offline Member
    Join Date
    Feb 2009
    Posts
    40
    Rep Power
    0

    Default

    I have a couple of follow-up questions. My end goal is to create an applet class that has its interface created by another class. From what I understand, JFrame, JApplet, etc. all rely on the Root Pane to manage their content pane. So via the Root Pane I should be able to access the content pane and add which ever components I'd like.

    • Does my approach/theory make sense?
    • What will I have to change to make my UserInterface class work as described above?

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Hm, not quite. You will want to place your Swing widgets into a JPanel likely and the Jpanel will be placed into the JApplet or JFrames contentPane, not the rootPane.

  12. #12
    dan0 is offline Member
    Join Date
    Feb 2009
    Posts
    40
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Hm, not quite. You will want to place your Swing widgets into a JPanel likely and the Jpanel will be placed into the JApplet or JFrames contentPane, not the rootPane.
    Placing the JPanels in the JApplet (or JFrame, etc.) contentPane is what I'm confused about. For example, in my UserInterface code I have the following components:

    Java Code:
    JPanel menuPanel = new JPanel();
    JPanel contentPanel = new JPanel();
    JPanel selectionPanel = new JPanel();
    Let's say I change TestUserInterface to:

    Java Code:
    public class TestUserInterface extends JApplet{
    	public static void main(String[] args){
    		UserInterface ui = new UserInterface(mainContainer);
    	}
    } // end TestUserInterface class
    Since UserInterface doesn't extend JApplet I can't use the add method, so how do I add the JPanels?

  13. #13
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Most of my GUI classes don't extend JFrame, JPanel, or other components. I try to only extend these classes if my class alters its basic behavior somehow. Otherwise, I'll have my class produce a JPanel or other component on request. Hang on a sec and I'll see if I can dig up an example.

  14. #14
    dan0 is offline Member
    Join Date
    Feb 2009
    Posts
    40
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Most of my GUI classes don't extend JFrame, JPanel, or other components. I try to only extend these classes if my class alters its basic behavior somehow. Otherwise, I'll have my class produce a JPanel or other component on request. Hang on a sec and I'll see if I can dig up an example.
    Any luck with finding an example? In the meantime, I put together the following code. Is it the proper way to have one class create a GUI for another applet class?

    Java Code:
    package projects.web;
    
    import java.awt.*;
    import javax.swing.*;
    
    public class UserInterface{
    	
    	JPanel buttonPanel = new JPanel();
    	JButton saveButton = new JButton("Save");
    	
    	public UserInterface(JRootPane topContainer){
    		buttonPanel.add(saveButton);
    		topContainer.getContentPane().add(buttonPanel);
    	}
    } // end UserInterface class
    Java Code:
    package projects.web;
    
    import javax.swing.*;
    
    public class TestUserInterface extends JApplet{
    	
    	JRootPane topContainer;
    	
    	public void init(){
    		topContainer = this.getRootPane();
    		UserInterface ui = new UserInterface(topContainer);
    	}
    	
    } // end TestUserInterface class

  15. #15
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    I'm wondering whether you're doing things a little backwards here. You would normally have a class create a JPanel and then the root component that displays that will get the JPanel, place it in the root's contentPane, and display it. You seem to be trying to have the JPanel request the object that's supposed to be displaying it. I'm trying to think of situations where you'd need that set up, and I can't think of any right now.

    Anyway, here's an example based on code created earlier:

    QueryComponent2.java
    creates a JPanel that displays a bunch of textfields and labels and can be used to extract text info out of the fields
    Java Code:
    import java.awt.BorderLayout;
    import java.awt.GridLayout;
    import java.util.HashMap;
    import java.util.Map;
    
    import javax.swing.BorderFactory;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JComponent;
    import javax.swing.JTextField;
    
    public class QueryComponent2
    {
      private static final int FIELD_LENGTH = 12;
      private static final int MID_GAP = 25;
      private static final int LINE_GAP = 10;
      private static final int BORDER_GAP = 10;
      private JPanel mainPanel = new JPanel();
      private Map<String, JTextField> fieldMap = new HashMap<String, JTextField>();
      private String[] labelStrings;
    
      public QueryComponent2(String[] labels)
      {
        JPanel labelPanel = new JPanel(new GridLayout(0, 1, 0, LINE_GAP));
        JPanel fieldPanel = new JPanel(new GridLayout(0, 1, 0, LINE_GAP));
    
        labelStrings = new String[labels.length];
        System.arraycopy(labels, 0, labelStrings, 0, labels.length);
        for (String string : labels)
        {
          labelPanel.add(new JLabel(string));
          JTextField textField = new JTextField(FIELD_LENGTH);
          fieldPanel.add(textField);
          fieldMap.put(string, textField);
        }
    
        mainPanel.setBorder(BorderFactory.createEmptyBorder(BORDER_GAP, BORDER_GAP,
            BORDER_GAP, BORDER_GAP));
        mainPanel.setLayout(new BorderLayout(MID_GAP, 0));
        mainPanel.add(labelPanel, BorderLayout.WEST);
        mainPanel.add(fieldPanel, BorderLayout.CENTER);
      }
    
      public String getFieldText(String key)
      {
        return fieldMap.get(key).getText();
      }
    
      // get the mainPanel to place into a JOptionPane
      public JComponent getComponent()
      {
        return mainPanel;
      }
    }
    QueryComponentMain.java
    creates a JPanel that holds a button. When pressed, this will display a JDialog with fields based on the Strings in the FIELD_NAMES array
    Java Code:
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import javax.swing.JButton;
    import javax.swing.JOptionPane;
    import javax.swing.JPanel;
    import javax.swing.JComponent;
    
    public class QueryComponentMain
    {
      private static final String[] FIELD_NAMES =
      { "First Name", "Second Name", "Phone Number", "What the Heck", "DILLIGAF",
          "Monday", "Tuesday", "Wednesday" };
      private JPanel mainPanel = new JPanel();
    
      public QueryComponentMain()
      {
        JButton getNamesBtn = new JButton("Get Names");
        getNamesBtn.addActionListener(new ActionListener()
        {
          public void actionPerformed(ActionEvent e)
          {
            getNamesAction();
          }
        });
        mainPanel.add(getNamesBtn);
      }
    
      // occurs when button is pressed
      private void getNamesAction()
      {
        // create object to place into JOptionPane
        QueryComponent2 queryComp = new QueryComponent2(FIELD_NAMES);
    
        // show JOptionPane
        int result = JOptionPane.showConfirmDialog(mainPanel, queryComp
            .getComponent(), // place the component into the JOptionPane
            "Get Names", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
        if (result == JOptionPane.OK_OPTION) // if ok selected
        {
          // query the queryComp for its contents by calling its getters
          for (int i = 0; i < FIELD_NAMES.length; i++)
          {
            System.out.println(FIELD_NAMES[i] + ": "
                + queryComp.getFieldText(FIELD_NAMES[i]));
          }
        }
      }
    
      public JComponent getComponent()
      {
        return mainPanel;
      }
    }
    QueryComponentFrame.java
    create a JFrame and place the QueryComponentMain's JPanel inside of it.
    Java Code:
    import javax.swing.JFrame;
    
    public class QueryComponentFrame
    {
      private static void createAndShowUI()
      {
        JFrame frame = new JFrame("Query Component");
        frame.getContentPane().add(new QueryComponentMain().getComponent());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
      }
    
      public static void main(String[] args)
      {
        java.awt.EventQueue.invokeLater(new Runnable()
        {
          public void run()
          {
            createAndShowUI();
          }
        });
      }
    }
    QueryComponentApplet.java
    create a JApplet and place QueryComponentMain's JPanel inside of it's contentPane and display it
    Java Code:
    import javax.swing.JApplet;
    import javax.swing.SwingUtilities;
    
    public class QueryComponentApplet extends JApplet
    {
      private void createGUI()
      {
        getContentPane().add(new QueryComponentMain().getComponent());
      }
      
      @Override
      public void init()
      {
        try
        {
          SwingUtilities.invokeAndWait(new Runnable()
          {
            public void run()
            {
              createGUI();
            }
          });
        } 
        catch (Exception e)
        {
          e.printStackTrace();
        } 
      }
    }

  16. #16
    dan0 is offline Member
    Join Date
    Feb 2009
    Posts
    40
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    I'm wondering whether you're doing things a little backwards here. You would normally have a class create a JPanel and then the root component that displays that will get the JPanel, place it in the root's contentPane, and display it. You seem to be trying to have the JPanel request the object that's supposed to be displaying it. I'm trying to think of situations where you'd need that set up, and I can't think of any right now.
    Thank you for the examples; they helped me understand how to add components to a target root's contentPane.

    For future reference, is there a best practice when it comes to adding components to a container? Is it bad practice to have the component request the container that's supposed to hold it?
    Last edited by dan0; 03-02-2009 at 09:25 PM.

  17. #17
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Please know that I'm not a pro, so take my recommendations for what they are: hobbiest recommendations, but I do have my component classes sometimes request a reference to the container that holds them, but the trade-off is that this increases coupling, something we try to avoid doing.

  18. #18
    dan0 is offline Member
    Join Date
    Feb 2009
    Posts
    40
    Rep Power
    0

Similar Threads

  1. Replies: 9
    Last Post: 10-18-2008, 08:26 PM
  2. 'Cannot find symbol' error
    By minihazard10 in forum New To Java
    Replies: 6
    Last Post: 10-10-2008, 04:05 AM
  3. Programm Error: cannot find symbol Help?
    By junix in forum New To Java
    Replies: 2
    Last Post: 12-10-2007, 05:30 AM
  4. Error: cannot find symbol
    By silvia in forum New To Java
    Replies: 1
    Last Post: 08-07-2007, 05:39 AM
  5. Error: cannot find symbol
    By cachi in forum AWT / Swing
    Replies: 1
    Last Post: 08-06-2007, 08:12 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •