Closed Thread
Results 1 to 3 of 3

Thread: Package error (Not exist/Wrong name)

  1. #1
    Cander's Avatar
    Cander is offline Learning Programmer
    Join Date
    Mar 2009
    Location
    Sweden
    Posts
    63
    Rep Power
    0

    Package error (Not exist/Wrong name)

    Hello,

    I have trying to sorting my classfiles in my project into folders and using the Java Package function, but I'm experiencing problems nevertheless after trying to following any tutorials about this I could find.

    I made a very simple test just to trying to get this work, here is what I got:

    test1.java in folder C:\mainn

    Code:
    package mainn;
    import mainn.sub.test2;
    public class test1
    {
    	public static void main(String[] arguments)
    	{
    	}
    }
    test2.java in folder C:\mainn\sub

    Code:
    package mainn.sub;
    public class test2
    {
    }
    test2.java compiles, but when compiling test1.java I receive the following error:
    Code:
    test1.java:2: package mainn.sub does not exist
    import mainn.sub.test2;
                    ^
    1 error
    If I remove the import statement, it compiles. But when running test1.java I receive this error:
    Code:
    java.lang.NoClassDefFoundError: test1 (wrong name: mainn/test1)
    	at java.lang.ClassLoader.defineClass1(Native Method)
    	at java.lang.ClassLoader.defineClass(Unknown Source)
    	at java.security.SecureClassLoader.defineClass(Unknown Source)
    	at java.net.URLClassLoader.defineClass(Unknown Source)
    	at java.net.URLClassLoader.access$000(Unknown Source)
    	at java.net.URLClassLoader$1.run(Unknown Source)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.net.URLClassLoader.findClass(Unknown Source)
    	at java.lang.ClassLoader.loadClass(Unknown Source)
    	at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    	at java.lang.ClassLoader.loadClass(Unknown Source)
    	at java.lang.ClassLoader.loadClassInternal(Unknown Source)
    Could not find the main class: test1.  Program will exit.
    Exception in thread "main" >Exit code: 1
    What am I doing wrong?

    Thanks in advance for help!

    //Cander

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Join Date
    Jul 2009
    Location
    Santa Clarita, CA
    Posts
    2,111
    Blog Entries
    47
    Rep Power
    31

    Re: Package error (Not exist/Wrong name)

    This is because the mainn folder is NOT on the classpath. In order to build it using javac, you'll need to go outside the mainn folder and say this:
    Code:
    javac mainn\test1.java
    That should work. To run it, you'll also need to run it outside the package folder, like so:
    Code:
    java mainn.test1
    This is because Java will automatically append the current directory to the classpath, which will then properly resolve the position mainn.sub.test2 as the class.
    Wow I changed my sig!

  4. #3
    Cander's Avatar
    Cander is offline Learning Programmer
    Join Date
    Mar 2009
    Location
    Sweden
    Posts
    63
    Rep Power
    0

    Post Re: Package error (Not exist/Wrong name)

    Thanks, that helped! I didn't really understand how classpaths worked and how to compile/run the classes properly.
    Last edited by Cander; 03-02-2010 at 12:02 PM.

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. I'm Getting A Syntax Error; Anyone Know What's Wrong?
    By RhetoricalRuvim in forum C and C++
    Replies: 5
    Last Post: 06-28-2011, 01:28 AM
  2. Does such a program exist?
    By alazanski in forum General Programming
    Replies: 3
    Last Post: 01-29-2011, 11:30 AM
  3. What's wrong with this perl error
    By hhheng in forum Perl
    Replies: 1
    Last Post: 02-14-2009, 09:54 AM
  4. if dir exist command?
    By phpforfun in forum PHP Development
    Replies: 4
    Last Post: 02-17-2008, 09:38 PM

Tags for this Thread

Bookmarks

Posting Permissions

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