Command line compilation

I am trying to understand how the -classpath option -classpath when compiling from the command line.

I am trying to create a parent mydirectory element:

javac -cp mydirectory / subdir Hello.java

But the compiler says:

javac: no source files

How does -cp ( -classpath ) work? What am I doing wrong?

If I do this from the subdir directory:

javac Hello.java

then compiles correctly.

+4
source share
3 answers
 javac TestProgram1.java TestProgram2.java TestProgram3.java 

You can use a wildcard to compile all the files in a folder, for example:

 javac *.java 

If you need to compile a large number of files at the same time, but do not want to use a wildcard (perhaps you want to compile a large number of files, but not all the files in a folder), you can create an argument file that lists the files to compile. In the arguments file, you can enter as many file names as you want, using spaces or line breaks to separate them. Heres an argument file called TestPrograms, which lists three files to compile:

 TestProgram1.java TestProgram2.java TestProgram3.java 

You can compile all the programs in this file with the @ symbol, followed by the name of the argument file on the javac command line, for example:

 javac @TestPrograms 

-cp and -classpath

Determines where to look for user class files. Use this option if your program uses class files that you saved in a separate folder.

+1
source

The files you compile must be defined directly

For example, if you are in the parent folder:

 javac subdir/Hello.java 

required to compile.

The class path allows you to find the .class files needed to compile the things mentioned above.

For example, if in the code you had a link to the "Birds" class, and this class was in the jar named "animal.jar" in the current folder, which was the parent location where the java file was, you would need the following:

 javac -cp animals.jar subdir/Hello.java 
0
source
 javac XXX.java 

The command used to compile the java class, where as The -classpath flag is available using the java command.

we run a java program using

 java XXX.class 

here, to start with the main () function, the JVM needs to know where the class file is. therefore, to indicate the class path, we use the classpath flag

Classpath to get more details.

As an example, to run a java program, there is

 java -classpath .;yourJarFile.jar your.MainClass java -classpath path_till_classfile/HelloWorld 
0
source

Source: https://habr.com/ru/post/1494345/


All Articles