Results 1 to 6 of 6
  1. #1
    Splat is offline Member
    Join Date
    Oct 2009
    Posts
    21
    Rep Power
    0

    Default can't import my own class

    Hi,

    I am a complete noob when it comes to Java and so I hope someone can help me here. I am not new to programming. Have used C++, Python, Ruby (somewhat), Delphi, etc. However, I feel like I am having to make a bit of paradigm shift when it comes to Java. I am using Java on Windows XP basically from the command line without an IDE. (I don't think I've got enough RAM to run an IDE at the moment, only around 480MB).

    I am basically trying to find out how to 'import' my own classes. None of my random guesses is bearing any fruit. I am imaging that

    import packagename;

    should do the trick but I keep getting 'bad class file' error when trying to do the import. Someone told me that I have to use the right directory structure but that I am confused about. I am confused about whether case matters on windows directory names or only file names?? I understand that package names are lowercase by convention. Do I have to pass anything special on the javac compiler command line to import the extra class?

    It seems that every irc channel that I come across hates java newbies so your help would be greatly appreciated.

    Regards
    Glenn.

  2. #2
    Rhez is offline Member
    Join Date
    Oct 2009
    Posts
    17
    Rep Power
    0

    Default

    So, you want to import your own classes from one project to another (or copy to another project to leave no changes in that project).

    1. Look for your projects - they should look like folders
    2. Click into one of them where you want your class
    3. The class has the file name of ".java"; select it but don't open it
    4. Press ctrl+c or right click and select "Copy"
    5. Go back and click into the other project where you want your class to be imported
    6. Press ctrl+v or right click in and empty space of the folder and select "Paste"

    Open your project; it should be there, I hope

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

    Default

    Copying and pasting files will work but is not a robust long-term solution. The best solution is to be sure that the packages of the class files that you're trying to use are on the current classpath. Take a look at the Java tutorials and also search here and the forums.sun.com regarding classpath for much more information on this.

  4. #4
    Splat is offline Member
    Join Date
    Oct 2009
    Posts
    21
    Rep Power
    0

    Default

    Ok, my apologies if I've been a little unclear. I've prepared a minimal test case to illustrate my problem.

    In App1.java

    Java Code:
    import class1.*;
    
    public class App1
    {
    
    	public static void main(String[] args)
    	{
    	}
    }
    In Class1.java
    Java Code:
    package class1;
    
    public class Class1
    {
    	int a = 23;
    }
    Both of these files are in the same directory. Do they need to be in different directories? If so, what should the directories be called and is the spelling of the directory case sensitive?

    Class1.java compiles ok. App1.java does not. I am wondering if its either a case sensitivity problem or directory layout problem. However the java tutorial at java.sun.com seemed to suggest that it wasn't.

    This is what happens when I try to compile App1.java
    Java Code:
    C:\Projects\Java\Minimal>javac App1.java
    App1.java:1: package class1 does not exist
    import class1.*;
    ^
    1 error
    There are just so many things I could be doing wrong and I don't know where to start. Anyway I hope this example helps clarify my problem.

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Yes, create a folder name 'class1' in your project folder and add your class Class1.java in that folder.

  6. #6
    Splat is offline Member
    Join Date
    Oct 2009
    Posts
    21
    Rep Power
    0

Similar Threads

  1. Eclipse 3.5<import *.jar -Need help>
    By roughjj in forum Eclipse
    Replies: 1
    Last Post: 09-09-2009, 09:24 AM
  2. How could i import DLL in NetBeans?
    By makpandian in forum NetBeans
    Replies: 0
    Last Post: 02-13-2009, 12:53 PM
  3. Replies: 4
    Last Post: 12-29-2008, 02:20 PM
  4. Import not working
    By ribbs2521 in forum New To Java
    Replies: 5
    Last Post: 12-16-2008, 06:14 AM
  5. import statement.
    By diRisig in forum New To Java
    Replies: 2
    Last Post: 02-08-2008, 12:34 AM

Posting Permissions

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