aspose file tools*
The moose likes Beginning Java and the fly likes Package Compilation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Package Compilation" Watch "Package Compilation" New topic
Author

Package Compilation

Eden Landau
Ranch Hand

Joined: Aug 14, 2011
Posts: 40

I'm just (after much postponing) got into using packages. I can get them to work just fine, unless I try to tinker the default directory structure.

What I got to work is just typing down "javac -d . foo.java", but not anything like "javac -d ../../foo/foobar foo.java". That is not to say it wouldn't compile, but nothing in said package is in the other classes' scope.

What am I doing wrong?

Blessed be,
Eden.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  17

Hi Eden.

Did you realize that the -d switch specifies where to output .class files? It has nothing to do with packages.

Maybe you can explain in greater detail what you're trying to accomplish, and what's happening instead?
Eden Landau
Ranch Hand

Joined: Aug 14, 2011
Posts: 40

Perhaps my understanding of scopes is lacking, then.

My source files are located in "src\practice\dice\console", and I want them to compile to "dir\practice\dice\console".
All of the files are in a package called "practice.dice.console".

When I compile any independent class (namely, an interface that doesn't implement or extends anything), it compiles normally, and without problems.
However, when I try to compile a class that implements said interface, it claims it "cannot find symbol".

Again, this does not happen when I compile them to where I keep my source files, nor does it happen when I compile it using "javac -d . foo.java".

And thank you for the quick reply!

Blessed be,
Eden.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  17

What happens is that Java looks for the source file you want to compile, *exactly* where you tell it. If you say javac Foo.java, it will look for Foo.java in the current directory. Files referenced from Foo.java are a different story though. All referenced files will be looked for on the classpath. By default, the classpath is the current working directory. If you have the CLASSPATH variable set in your environment variables, it will look for referenced files there. If you compile with the -classpath switch, it will look for referenced files there, overriding the CLASSPATH variable.

When I compile using the command prompt, I always go to my project folder containing my src folder, and I use the following command: "javac -classpath src -d bin src\com\example\MyClass.java". This will find MyClass.java in the src/com/example folder, and then find referenced classes under src. It doesn't matter if they are .class or .java files. Then all classes that need to be compiled will go in the bin folder, under the proper package structure.
Eden Landau
Ranch Hand

Joined: Aug 14, 2011
Posts: 40

And now it works. Thank you ever so much!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Package Compilation