Results 1 to 4 of 4
  1. #1
    dineroking is offline Newbie
    Join Date
    Apr 2013
    Location
    Houston
    Posts
    2
    Rep Power
    0

    Default creating packages from Eclipse

    I'm learning Java and using a book called Java Illuminated. The book has me use the convention to create a package, say "package com.myDomain.test;". I created "C:\MyFiles\Java\Packages\com\myDomain\test\Class. java". I went on the command line and compiled the class in the above directory. I also added "C:\MyFiles\Java\Packages\" to the system variable CLASSPATH, but in Eclipse I e get an error message that says 'The declared package "com.myDomain.test" does not match the expected package "" '. I should also point out that when I originally create the classes I create them inside a directory that is inside of dropbox but I did copy the class into the directory mentioned at the beginning. I'm trying create packages to store my classes so that I can import them from any class regardless of where the project is.

    Thank you in advance for your help.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default Re: creating packages from Eclipse

    A side point, but "Class" is not a good name for a class. For a couple of reasons: there is already a standard class called Class. Having duplicate names is OK (that's one reason for packages) but it can be confusing. The other reason is that it isn't a very descriptive name. Variables, class and packages should all be as descriptive as possible as that helps make the code clear (to us and to you in 6 months time).

    ---

    If the Eclipse compiler is grumbling about the package the chances are you haven't got a package statement as the first line of the .java file. Eclipse will take that as meaning "" (the default package), but you have put the .java file inside a package "com.myDomain.test" so it objects about the mismatch.

  3. #3
    dineroking is offline Newbie
    Join Date
    Apr 2013
    Location
    Houston
    Posts
    2
    Rep Power
    0

    Default Re: creating packages from Eclipse

    Hello, thank you for your answer.
    I agree with you that Class would be a confusing name for a class. I just used that name for reference here in the forum.

    I did include the package statement at the beginning of the class file and copied the class into the folder where the package is but I still get the error message.
    I also added the directory where the package is into the CLASSPATH, but it seems like the java compiler is not looking in that directory.
    Please let me know of any suggestions.
    Thank you.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default Re: creating packages from Eclipse

    The CLASSPATH environment variable only has any effect when compiling from the command line, Eclipse doesn't use it.

    Eclipse will maintain directories to hold java source and class files to maintain the proper directory structure. Don't copy files yourself (in case you copy them to the wrong place), instead, in the "Project Explorer" pane of Eclipse create the package com.myDomain.test in whatever project you re using. Then, within that package create the class Class. Open the source file in Eclipse and replace its contents (if it has any) with the code you want. ie copy and paste it into the Eclipse editor window.

    ---

    Note that Eclipse adds a layer of complexity (projects) and obscurity (the directory structure) that is missing from "raw Java" at the command line. For that reason many people recommend that you use the command line until you are familiar with the more common command line options of the java and javac executables.

Similar Threads

  1. Replies: 1
    Last Post: 11-19-2012, 11:58 AM
  2. How do I import foreign packages to eclipse?
    By EscSequenceAlpha in forum Eclipse
    Replies: 1
    Last Post: 04-06-2012, 04:14 AM
  3. creating war file in eclipse
    By pelegk1 in forum Eclipse
    Replies: 3
    Last Post: 05-24-2010, 05:28 AM
  4. Creating and Using Packages
    By dawiz001 in forum New To Java
    Replies: 18
    Last Post: 02-23-2009, 01:58 AM
  5. Replies: 3
    Last Post: 02-12-2008, 12:03 PM

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
  •