Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Package Compilation

 
Eden Landau
Ranch Hand
Posts: 40
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 6113
72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 40
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 6113
72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 40
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And now it works. Thank you ever so much!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic