Volodymyr Levytskyi
Volodymyr Levytskyi

Reputation: 3432

How to compile java files into directory at the same level as java files?

I have very simple case. In folder D://redbacktree I have two files .java.

I want to compile them into folder D://redbacktree/bin.

What I tried is :

javac -d bin *.java

This created folder redblacktree inside bin folder and put .class files into D://redbacktree/bin/redblacktree/

But I don't want javac to create redblacktree folder inside bin folder.

How to compile and run this correctly?

Upvotes: 0

Views: 417

Answers (2)

Peter Svensson
Peter Svensson

Reputation: 6173

I'll create an answer from my comment instead. So first off; if you have package declarations (which is good practice to organize your code and modules) your files will end up in a folder structure matching your package structure.

I'd suggest reading the man pages for javac and javato see all the flags and options. I'd also suggest using some kind of build tool and follow the java "convention" of how to organize your source and class files. Read more here.

Maven, Gradle or Buildr are three examples of build tools for Java.

Too "fix" your current problem you can run your compiled java classes using:

java -cp bin redblacktree.<name of your class>

i.e you need to use the fully qualified name (package path + class name) when telling java which class to run. Also -cp specifies the (root-)classpath, where your compiled classes can be found.

Upvotes: 1

Jack
Jack

Reputation: 3059

Maybe your java files have a package declaration?

package redbacktree;

The compiler creates a separate folder for each package. You could try to use the default package (i.e. omit the package declration completely), although it is not a good practice to use the default package.

Upvotes: 1

Related Questions