Results 1 to 14 of 14
  1. #1
    fgm1 is offline Member
    Join Date
    Nov 2009
    Posts
    23
    Rep Power
    0

    Default Problem using packages.

    Greetings.

    OK, I am using Notepad++ to do my coding. I've been using java for a few years, till now I haven't concerned myself with packages and specifying CLASSPATH because I haven't needed them, my programs are self contained, the files are all grouped together, and I compile and run from the command line.

    I understand that packages are essentially a mechanism for organizing classes, and that a CLASSPATH tells the JVM where to find user defined classes and packages.

    OK, so I take my program and add a line package stuff; to the top of each file.

    Ok I am surprised to learn that the only way I can run the program is from c:\ and specifying the fully qualified name of the main class i.e

    java stuff.MyProg

    I can't run the program from the directory where it is stored, and using a CLASSPATH eqial to the where the program is stored doesn't seem to make any difference to anything, I still need the fully qualified name.

    AM I making myself clear? Is this what is supposed to happen? I did some poking around in Google, and the SUn java Tutorial, but couldn't answer this specific question. I realize that there is no value added to using packages and classpath under these conditions, but still I am surprised it doesn't work.

    Thanks.

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Of course packages work and are very important. That's why all the systems you know written in Java use them.
    If you are getting errors then post your steps and the errors you get.
    If you read the tutorials you will be told that the classes must be placed inside folders that match the package structure as well.

  3. #3
    fgm1 is offline Member
    Join Date
    Nov 2009
    Posts
    23
    Rep Power
    0

    Default

    I understand the value of packages, I'm just a little surprised at what happened in this case.

    I mentioned my steps, but i'll rephrase.

    All my source and class files for this program are stored in the same directory, c:\stuff. So I don't actually need to specify a CLASSPATH or a package, things work just fine without that. That is what I mean by value added, there isn't any in this particular situation.

    But I decide to use one anyway, as a starting point for bigger things.

    so i call the package stuff and add the line package stuff; to the top of each file for my program.

    As a result I can't run the program from the c:\stuff directory, either by just specifying the c:\stuff>java MyProg, or the fully qualified name c:\stuff>java stuff.MyProg In both cases I get the exception NoClassDefFoundError

    I can however specify the fully qualified name from the root of the c drive. i.e.

    c:\>java stuff.MyProg and it works.

    furthermore, specifying CLASSPATH=c:\stuff; doesn't seem to change anything.

    What I haven't been able to deduce from the tutorials I have read is whether this situation is working as intended. I guess I had not expected that adding a package statement to all my files would prevent me from running the program from the directory in which it is stored.

    regards.

  4. #4
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    5

    Default

    yes, adding a package to your .java files (and moving them into the folder of the same path as the package name -java is good to enforce package name == folder name). - requires you to now refer to all your classes from the 'top level' of the package structure.

    i used to find it confusing how javac lets you cd into the stuff folder, and javac MyProj.java in that folder, but running it must be from the top of the class path structure.

    when you set the classpath, did you set it as an environment variable. for example, did "echo %CLASSPATH% show you c:\ ?

    in your case, if you have package 'stuff' then the correct way to invoke it would be to invoke all java things from the top of the class path, and to use the dot ( . ) as package delimiters, not the path separator.

    so either in the top level folder,

    Java Code:
    C:\java stuff.MyProj
    or with CLASSPATH set as environment variable

    Java Code:
    set CLASSPATH=C:\
    java stuff.MyProj
    or, i prefer to always specify the classpath inline with the java command

    Java Code:
    C:\someotherfolder> java -classpath c:\  stuff.MyProg
    where the "-classpath" can also be shortened to "-cp"
    and if we ever have more than one thing on the classpath, such as a .jar file, or a different folder, (c:\morestuff) we need to specify these using the path separation character, which on windows is painfully the semicolin ( ; ) and on unix is the colin ( : )

    for example,
    Java Code:
    java -cp c:\;c:\libs\commons-logging.jar  stuff.MyProg
    Last edited by travishein; 11-03-2009 at 08:19 PM.

  5. #5
    fgm1 is offline Member
    Join Date
    Nov 2009
    Posts
    23
    Rep Power
    0

    Default

    Ok thanks travishein, that helps. I suppose one thing I was doing wrong was setting my classpath improperly. I was setting it to c:\stuff, the actual folder where MyProg lived. By setting classpath to c:\ I could then run stuff.MyProg from any directory. I guess it stands to reason that the fully qualified name should mirror the path to the program relative to the classpath directory.

    I'll take some time to absorb the rest of your post, and reply back if I have any more questions. Thanks again.

  6. #6
    fgm1 is offline Member
    Join Date
    Nov 2009
    Posts
    23
    Rep Power
    0

    Default

    Okay, as I dig more into the topic, I can see that one of the big advantages of packages is that by organizing your pre-existing classes into packages, and by making sure your package directories are in the class path. You can design new applications that make use of these classes by using the import statement in the new application, rather than making a copy of the class each time you want to use it. At least I think that's the point.

    But what if you want to give your application to someone else? By what means to they get the needed classes from these pre-existing packages. Would they need a CLASSPATH and directory structure that matches what you have used, or perhaps there is a compile option that actually brings in a copy of those classes that are needed for the new application.

    Till now, as an amateur programmer, I have just thrown all my class files into one directory and created a jar file that I can send to someone else.

  7. #7
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Same. You just create a jar file from the classes and give them the jar file.

  8. #8
    fgm1 is offline Member
    Join Date
    Nov 2009
    Posts
    23
    Rep Power
    0

    Default

    Quote Originally Posted by r035198x View Post
    Same. You just create a jar file from the classes and give them the jar file.
    Thanks, so that would suggest that the particular arrangement of CLASSPATH and packages I have on my development computer, when it comes time to use the program on another computer, I either have to duplicate that setup of packages and CLASSPATH, or else I have to gather up all my class files that I have used from the various packages. Which is one of the things using packages and CLASSPATH has eliminated the need for in the first place.

    It's not that I have a problem per se with manually gathering the class files and putting them in a jar file, it's that intuition tells me that this is kind of a clumsy way of dealing with it.

    So I am wondering how a real developer, which I am not, who writes and distributes lots of applications, deals with this.

  9. #9
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    I don't get where your problem really lies with this. Are you saying it's better to distribute class files than .jar files?
    Also when you didn't put your classes in a package before it's not like they were not in a package. Java puts all those classes in one package (the default package).

  10. #10
    fgm1 is offline Member
    Join Date
    Nov 2009
    Posts
    23
    Rep Power
    0

    Default

    Quote Originally Posted by r035198x View Post
    I don't get where your problem really lies with this. Are you saying it's better to distribute class files than .jar files?
    Also when you didn't put your classes in a package before it's not like they were not in a package. Java puts all those classes in one package (the default package).
    Well, when I did it before, all of my own classes that any application used were always grouped in the same directory, so there was no need for packages, or additional classpath directories over and above the default that all java installations have. So far so good?

    So now I am getting my feet wet by writing applications that make use of my own packages and I have added to the default CLASSPATH according to the locations I have placed these custom packages.

    So my point is that it is no longer a question of relying on the default java packages and classpath. To give my program to someone else, It seems I have to do one of the two following things, agreed?

    1. make sure that the new user gets a classpath/package directory that is consistent with mine, or.

    2. manually gather up copies of the classes from the different package directories that are used in the application. Entirely doable.

    So given those two choices, it's not clear to me what the accepted strategy is.

  11. #11
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    5

    Default

    So I am wondering how a real developer, which I am not, who writes and distributes lots of applications, deals with this.
    As a real developer, which I am, I use the Ant build utility, which is available from ant.apache.org

    It uses an xml file to define targets, within each target, can have actions.

    So I make a project 'module' folder contain a top level build.xml, then a src folder for all of my java sources (and those packages)

    Then I have a compile target to find all java sources and compile them for me, placing the outputs into a build/classes folder.

    And a second target "lib" which will automatically run the compile target if required, taking the built classes and creating a .jar file, into a target/ folder

    Java Code:
    <project name="myproject" default="lib" description="a sample ant build.xml file">
    	<property name="jarfile" value="myproject.jar" description="set to the name of the jar file we want created"/>
    		
    	<target name="compile" description="compiles all Java sources.">
    		<mkdir dir="build/classes"/>
    	  <javac source="src" target="build/classes"/>
    	</target>
    	
      <target name="lib" depends="compile" description="package a jar file of the built classes">
        <mkdir dir="target"/>
      	<jar    jarfile="target/${jarfile}">
    		  <fileset dir="build/classes">
    			  <include name="**/**"/>
    			</fileset>
        </jar>
      </target>
    	
    	<target name="clean" description="removes built outputs">
    	  <delete dir="build" failonerror="false"/>
        <delete dir="target" failonerror="false"/>
    	</target>
    </project>
    And if i distribute the module as source code (such as in a version control system, i set the repository to ignore the build and target folders, and so the src folder and the build.xml are all any other developer needs to take my project and build a .jar file from it.

    and if i wanted to distribute the jar file, it is understood, or covered in the documentation release notes that the jar file needs to be added to the classpath somehow. which is dependent on the operating environment (a web app container, another application, a stand alone application).

    but using the ant build approach removes all the manual (and error-prone) operations of compiling and packaging things. A well set up ant build file can sometimes take me a while to get all the kinks out, but once it works consistently its one of those things I can set and forget.
    Last edited by travishein; 11-05-2009 at 04:58 PM.

  12. #12
    fgm1 is offline Member
    Join Date
    Nov 2009
    Posts
    23
    Rep Power
    0

    Default

    Thanks for a detailed reply, i'll take a little time to digest it all. So far I haven't bothered with things like build utilities or IDE's, but if I am going to get serious about programming, I suppose i should make use of some of these tools.

    Anyways, your 2nd last paragraph seems to address my main question. It is reasonable to expect that the user may need to make changes to a CLASSPATH environment variable if they want to use your program.

    regards.

  13. #13
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Your installer should take care of the classpath not users. If you use a jar file then you can use the manifest to set some of these settings.
    If you write big programs then you really need separate packages. e.g if you use MVC for a desktop application then you'll want to put the model in a separate package from the view and controller bits. They each will have different dependencies too.

  14. #14
    fgm1 is offline Member
    Join Date
    Nov 2009
    Posts
    23
    Rep Power
    0

    Default

    Quote Originally Posted by r035198x View Post
    Your installer should take care of the classpath not users. If you use a jar file then you can use the manifest to set some of these settings.
    If you write big programs then you really need separate packages. e.g if you use MVC for a desktop application then you'll want to put the model in a separate package from the view and controller bits. They each will have different dependencies too.
    right, makes sense.

    OK thanks guys for the help, I consider my questions to be well answered.

Similar Threads

  1. Packages
    By urbim in forum Eclipse
    Replies: 2
    Last Post: 07-02-2009, 02:01 PM
  2. packages
    By fogus in forum New To Java
    Replies: 1
    Last Post: 03-24-2009, 06:14 AM
  3. Replies: 3
    Last Post: 02-12-2008, 11:03 AM
  4. packages problem
    By cew27 in forum New To Java
    Replies: 5
    Last Post: 01-10-2008, 09:14 PM

Posting Permissions

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