P
P
philX2015-03-21 14:30:03
Java
philX, 2015-03-21 14:30:03

Why is the second javac argument needed - classpath ./classes -d ./classes src/myproject/*.java?

I just started Java yesterday and stupid questions have already started :)
Everything is in order. I came across the fact that the files with examples from the book were compiled, but did not want to run. It was established by typing (since I didn’t have an Internet at that time) that when the package % packageName% lines are removed from the source, everything starts working normally. I know about packages and other similar concepts from other languages, so I decided that I need to figure it out, because it's not good to just remove such directives, you need to be able to use them. As a result, I entered the process of properly organizing this entire kitchen, now everything is correctly stored, compiled and working. But one nuance was not clear to me. Now I'm just going to show you how to do it.
$ mkdir HelloWorld // create a folder with the future project
$ cd HelloWorld
$ mkdir src/mypackage // create a folder for the sources, and in it a folder for the mypackage package
$ mkdir classes // compiled *.class files will be here
// now in the src/mypackage folder we place the source files included in this package, in each of which we specify package mypackage;
$ javac -classpath ./classes -d ./classes src/mypackage/*.java // as a result of compilation in the ./classes folder, the mypackage folder will appear, containing all the compiled class files
$ java -classpath ./classes mypackage.HelloWorld // we start the JVM and pass it the "main" application class. Everything is working. Everyone is happy.
It seems that I'm doing everything right (at least everything works correctly). But the command line itself, in which the compilation takes place, is not completely clear to me. Why do I specify the ./classes directory twice? When I specify it after the -d switch, I am saying that the finished compiled files should be placed in the ./classes directory, that is, it will be the root for the result. So? And what do I indicate by the first mention of the same directory immediately after the -classpath key? It is impossible not to specify it (I tried). At the same time, you can specify any left directory instead, and everything will work as before when I specified ./classes.
In general, if you are not too lazy to read all this and answer, I will be very grateful. I would not like to miss such nuances at first. After all, I didn’t use any development environment just for this, only to first correctly enter into such details.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
Cyril, 2015-03-21
@Sk1talec

First, the shorter alias -cp can be used instead of -classpath.
Secondly, with this directive you tell the compiler where to look for SOURCE files, class files and annotation files that your project depends on.
The standard approach to compilation is as follows.
After -cp you specify the folder where your source files are, and through ";" paths to .jar libraries.
After -d "./classes" the standard path for compiled files.
The last parameter is the path to the entry point of the program. A file containing
public static void main(String[] args) {}. For example src/ru/myproject/Main.java
More details in official javac documentation

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question