File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Package Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Package" Watch "Package" New topic
Author

Package

Bill Keller
Greenhorn

Joined: Aug 03, 2004
Posts: 9
I'm extending a 'main' program which begins with

package subDirA.subDirB

This program runs OK after a '.jar' file is created. However, during development I would prefer to run w/o the additional step of having to create a new jar before every test cycle. Unfortunately the 'package' statement must be commented out for that to succeed.

What do I need to do differently during testing to execute the .class version of my 'main' program ? I've tried every combination of -cp etc. that could possibly work, until nothing makes sense to me anymore.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

I'm assuming that your development environment is cmd.exe and notepad on Windows. One alternative, of course, is to use something a bit fancier, like the Eclipse IDE (www.eclipse.org). But it is nice to get your feet wet with simple tools first.

Anyway, let's say your directory tree looks like this:

C:\PROJECTS
C:\PROJECTS\subDirA
C:\PROJECTS\subDirA\subDirB
C:\PROJECTS\subDirA\subDirB\MyProgram.java

Here's what you do: in your console window, cd into C:\PROJECTS. In that directory, you can compile with

javac -classpath . subDirA\subDirB\MyProgram.java

and you can run with

java -classpath . subDirA.subDirB.MyProgram

and of course you can edit your source with

notepad subDirA\subDirB\MyProgram.java

as well.

Note that period (.) after the -classpath flag; that means "the current directory." In general, the class path should include the directories in which the top-level package directories of your classes appear. The default setting for the class path includes ".", so if your environment variable CLASSPATH is unset -- not set at all -- you can shorten the above to just

javac subDirA\subDirB\MyProgram.java

and

java subDirA.subDirB.MyProgram


[Jess in Action][AskingGoodQuestions]
Bill Keller
Greenhorn

Joined: Aug 03, 2004
Posts: 9
Bingo!!
You clearly typed java subDirA.subDirB.MyPgm
too many times I type java subDirA\subDirB\MyPgm
then Finally, I see/do as you advise, and of course it works

Can you explain why/how Java navigates from PROJECTS to locate files in subDirB? I would have never known to use the 'dot' format w/o your advice.

I'm modifying/extending Thinlet examples which deploy from ...thinlet\demo
I've used Eclipse with Ant build.xml for JSP/Struts projects for about a yr, but for this effort, I've simplified to cmd.exe and Notepad. I've never noticed the 'dot' format in a commandline before.

A big ThankS for the help !!
btw - have worked w/James Owens in Dallas using JESS, busns rules etc
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1815
The "java" command takes the name of a class -- not a class file. Remember that the package name is also part of the name of the class. Thus, if your class is "Grep" in package "foo.bar", then the actual class name is "foo.bar.Grep" This is what you type to run it. (java foo.bar.Grep)
Note that you do not type java foo/bar/Grep.class

This is also why the directory structure must match the package structure -- to find the .class file, the default ClassLoader simply substitutes '.' in the class name with the file separator for your OS ('/', '\', or even ':' (historically)). Then, is appends the ".class" extension and goes looking for that file. If it can;t find it -- ClassNotFoundException.


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

Originally posted by Bill Keller:

A big ThankS for the help !!
btw - have worked w/James Owens in Dallas using JESS, busns rules etc


Great, glad to help. Looks like Joel took care of your question. James has long been an asset to the Jess community and I bet he's interesting to work with.
Bill Keller
Greenhorn

Joined: Aug 03, 2004
Posts: 9
THIS PLACE IS UNMATCHED !!!
1. - You understand the questions
2. - You know the answers
3. - Your responses are prompt, timely
4. - the price is affordable

but the name/theme kept me out for a long time
- same mistake I made w/Yahoo & Google
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Package
 
Similar Threads
execute JAR from class
Development Chapter
call main-method inside jar
include jar in an executable jar
How to include my own package in jar file