Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By Tolls

Thread: import error

  1. #1
    oszc is offline Member
    Join Date
    Aug 2011
    Posts
    20
    Rep Power
    0

    Default import error

    hi,guys

    this is an example from book but i can not figure out. Please help.
    my package (Time1.class)is located in D:\Java\testImport\com\deitel\javafp\ch08
    i creat a new class (named Time1PackageTest.java) containing the main method in a different folder(D:\Java\testImport2).

    i cd to and execute D:\Java\testImport2>javac -cp D:\Java\testImport Time1PackageTest.java
    it seems to be ok.
    then i execute D:\Java\testImport2>java -cp D:\Java\testImport Time1PackageTest
    the error occur:
    Java Code:
    Exception in thread "main" java.lang.NoClassDefFoundError: Time1PackageTest
    Caused by: java.lang.ClassNotFoundException: Time1PackageTest
            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)
    Could not find the main class: Time1PackageTest.  Program will exit.
    then i execute D:\Java\testImport2>java Time1PackageTest

    error again:
    Java Code:
    Exception in thread "main" java.lang.NoClassDefFoundError: com/deitel/javafp/ch0
    8/Time1
            at Time1PackageTest.main(Time1PackageTest.java:9)
    Caused by: java.lang.ClassNotFoundException: com.deitel.javafp.ch08.Time1
            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)
            ... 1 more
    Time1 code:
    Java Code:
    package com.deitel.javafp.ch08;
    
    public class Time1 {
    	private int hour;
    	private int minute;
    	private int second;
    	
    	public void setTime(int h, int m, int s)
    	{
    		hour =( ( h>=0 && h < 24)?h:0 );
    		minute = ( (m>=0 && m<60)?m:0 );
    		second = ( (s>=0 && s<60)?s:0 );
    	}
    	
    	public String toUniversalString()
    	{
    		return String.format("%02d:%02d:%02d", hour, minute, second);
    	}
    	
    	public String to1String()
    	{
    		return String.format("%02d:%02d:%02d %s", ( (hour==0||hour==12)?12:hour%12 ), minute, second, hour>=12?"PM":"AM");
    	}
    }
    Time1PackageTest code:
    Java Code:
    import com.deitel.javafp.ch08.Time1;
    public class Time1PackageTest {
    
    	public static void main(String[] args) {
    
    		Time1 time1 = new Time1();
    		
    		System.out.println(time1.toUniversalString());
    	}
    
    }

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Type
    Java Code:
    java -cp . com/deitel/javafp/ch08/Time1PackageTest

  3. #3
    oszc is offline Member
    Join Date
    Aug 2011
    Posts
    20
    Rep Power
    0

    Default

    Quote Originally Posted by sunde887 View Post
    Type
    Java Code:
    java -cp . com/deitel/javafp/ch08/Time1PackageTest
    Time1.java in folder D:\Java\testImport
    Time1.class in folder D:\Java\testImport\com\deitel\javafp\ch08
    Time1PackageTest.class in folder D:\Java\testImport2

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    If you are in a parent directory then you would type
    Java Code:
    java -cp path\to\subdirectory className
    Make sure you get the path correct and that you have spaces in the correct spots.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,086
    Rep Power
    20

    Default

    Quote Originally Posted by oszc View Post
    then i execute D:\Java\testImport2>java -cp D:\Java\testImport Time1PackageTest
    the error occur:
    Java Code:
    Exception in thread "main" java.lang.NoClassDefFoundError: Time1PackageTest
    Caused by: java.lang.ClassNotFoundException: Time1PackageTest
            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)
    Could not find the main class: Time1PackageTest.  Program will exit.
    This is because you only put the directory for the com.deitel.javafp.ch08.Time1 class in the class path, and don't include the current directory (ie the one with the Time1PackageTest class). So it fails to find the Time1PackageTest class.

    Quote Originally Posted by oszc View Post
    then i execute D:\Java\testImport2>java Time1PackageTest

    error again:
    Java Code:
    Exception in thread "main" java.lang.NoClassDefFoundError: com/deitel/javafp/ch0
    8/Time1
            at Time1PackageTest.main(Time1PackageTest.java:9)
    Caused by: java.lang.ClassNotFoundException: com.deitel.javafp.ch08.Time1
            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)
            ... 1 more
    In this case you are running without any defined classpath so it defaults to current directory (.). This means it can find Time1PackageTest, since it is in the current directory, but now it can't find the com.deitel.javafp.ch08.Time1 class.

    You need both:
    -cp .;D:\Java\testImport
    oszc likes this.

  6. #6
    oszc is offline Member
    Join Date
    Aug 2011
    Posts
    20
    Rep Power
    0

    Default

    Thank you guys and special thanks to Mr.Tolls. Your answer is quite clear.

Similar Threads

  1. Import error for csv file in outlook
    By nida in forum New To Java
    Replies: 5
    Last Post: 09-07-2012, 01:22 AM
  2. error - I think involving my Import Statements
    By Sparky in forum New To Java
    Replies: 2
    Last Post: 02-02-2011, 02:31 PM
  3. Replies: 1
    Last Post: 01-04-2011, 11:16 AM
  4. error with import java.io.*
    By osval in forum New To Java
    Replies: 5
    Last Post: 05-13-2010, 05:03 PM
  5. import error
    By amarenash23 in forum New To Java
    Replies: 2
    Last Post: 01-15-2010, 11:32 AM

Tags for this Thread

Posting Permissions

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