Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Using a class

  1. #1
    Member Scotty's Avatar
    Join Date
    Oct 2010
    Posts
    60
    My Mood
    Scared
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Using a class

    Hiya,

    I am new to java, and have just started on classes. I am trying to do a project, however I cant find many resources on classes, and really need some help ASAP.

    I have written a class Time.java, however whenever I try to use it in another program (even just create an instance), I get the error:

    E:\java\test.java:4: cannot access Time
    bad class file: E:\java \Time.class
    unable to access file: E:\java \Time.class (The system cannot find the path
     specified)
    Please remove or make sure it appears in the correct subdirectory of the classpa
    th.
            Time update = new Time();
            ^
    1 error

    The classpath is correct and the class is in the right place. I need to get this error sorted quickly and dont have anyone else to ask. I am probably doing something very wrong and obvious, I dont really understand how to write and use my own classes as the book I have does not explain it very well.

    Any help/advice would be very helpful. Do I need to do something else to use it (import or something)? The class complies OK.

    Thanks,


  2. #2
    Junior Member
    Join Date
    Jan 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Using a class

    the space after java in path ?

    E:\java\Time.class - maybe like this ?

    Maybe the easiest way to use other classes is to keep them in the same folder as Your class with main method ?
    Last edited by xwid; January 29th, 2011 at 02:38 PM.

  3. #3
    Member Scotty's Avatar
    Join Date
    Oct 2010
    Posts
    60
    My Mood
    Scared
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: Using a class

    The space is just in the error message, not really there. I did not tell it where it was - it is in the same folder as the class with the Main method - it just says it cannot access it.

    E:\java \Time.class is correct, but I did not type that.

    Maybe an error in my class that stops it reading it?

    Thanks for your help

  4. #4
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Using a class

    • Are you using packages? If so, show the code of where you're declaring it.
    • Are classes and source files in the same directory? If not, where are they?
    • Try changing the class path in case its looking some where else
    • Delete and recompile a new .class file for the class calling Time.

    Just a few suggestions.
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  5. #5
    Member Scotty's Avatar
    Join Date
    Oct 2010
    Posts
    60
    My Mood
    Scared
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: Using a class

    no, its not in a package, just a file with the main file.


    Thanks
    Last edited by Scotty; January 31st, 2011 at 06:19 AM.

  6. #6
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Using a class

    1. What command-line command are you using?
    2. Could you post the code for your main class?

  7. #7
    Member Scotty's Avatar
    Join Date
    Oct 2010
    Posts
    60
    My Mood
    Scared
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: Using a class

    javac E:\myjava\test.java to run:

     
    public class test {
    	public static void main(String []args) {
     
    	Time display = new Time();
     
    	}
    }

    as I could not get to run in my main code I tried just a blank program with it in.

    javac E:\myjava\Time.java to compile class?

    Thanks

  8. #8
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Using a class

    E:\java\Time.class - maybe like this ?
    And
    E:\myjava\test.java
    are different directories.
    Or is that a mistype in your reply?
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  9. #9
    Member Scotty's Avatar
    Join Date
    Oct 2010
    Posts
    60
    My Mood
    Scared
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: Using a class

    Solved it, was a space after my enviro var for the classpath xD

Similar Threads

  1. any ways to run a class from another class?
    By javanub:( in forum Java Theory & Questions
    Replies: 3
    Last Post: May 9th, 2010, 06:57 AM
  2. Replies: 0
    Last Post: April 11th, 2010, 08:56 AM
  3. problem with data access when a class call another class
    By ea09530 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 4th, 2010, 05:20 PM
  4. Accessing a method of one class in another class
    By Sai in forum What's Wrong With My Code?
    Replies: 6
    Last Post: March 23rd, 2010, 04:06 PM
  5. how to load a class and type cast to that class?
    By chinni in forum Object Oriented Programming
    Replies: 2
    Last Post: November 9th, 2009, 10:18 AM