-2

I have trying to compile java files at the windows command line using commands such as:

java myProg once I have used javac to create class files.

Problems arise when I use packages with a number of source files.

Often but not always I get main not found errors even though a main exists.

I am not quite sure what some of the directives mean and that is why it seems hit or miss.

Question what does -cp mean exactly? java -cp src\myDirectory.myfile

sometimes I see:

./ infront of source eg .\src\myDirectory.myfile

on other sites I have found

% javac -cp .;stdlib.jar MyProgram.java

% java -cp .;stdlib.jar MyProgram

while compiling a jar library with java source files

what doesthe ".;" mean?

basically how do I compile three java source java files in one package at the windows command line and what does -cp and .; mean?

6
  • 1
    sergiy.ca/how-to-compile-and-launch-java-code-from-command-line take a look at this. I would still suggest that you use Ant or Maven Commented Oct 13, 2015 at 8:35
  • 1
    Have you seen this: How do I run java program with multiple classes from cmd? Commented Oct 13, 2015 at 8:41
  • It is a good starting point - what does the line if you have any other classes in the project , they should be automatically compiled and placed in the correct folders mean? Doe sit mean I should place them in the folders or the compiler will do it for me? This is the crux of the problem , I have three .java class files not one and this example only addresses one .java file. Commented Oct 13, 2015 at 8:43
  • -cp is classpath. . means current directory and ; is a separator of locations you are telling javac to look for in the classpath Commented Oct 13, 2015 at 8:44
  • Doe sthat mean ; means you are looking for more than one file in a folder? Commented Oct 13, 2015 at 8:46

2 Answers 2

0

-cp means class path if I'm not mistaken. try reading the following java docs

-classpath path Specifies the path javac uses to look up classes needed to run javac or being referenced by other classes you are compiling. Overrides the default or the CLASSPATH environment variable if it is set. Directories are separated by semi-colons. It is often useful for the directory containing the source files to be on the class path. You should always include the system classes at the end of the path. For example:

   javac -classpath .;C:\users\dac\classes;C:\tools\java\classes ...

https://www.cis.upenn.edu/~bcpierce/courses/629/jdkdocs/tooldocs/win32/javac.html

Sign up to request clarification or add additional context in comments.

Comments

-1

Answering your question directly, -cp means classpath or path.

Details on commandline arguments used while compiling and running a Java application can be found here: javac - Java programming language compiler

Extracting the description of -cp from that page:

-cp path or -classpath path:

Specify where to find user class files, and (optionally) annotation processors and source files. This class path overrides the user class path in the CLASSPATH environment variable. If neither CLASSPATH, -cp nor -classpath is specified, the user class path consists of the current directory. See Setting the Class Path for more details.

. means the current directory.

To compile multiple files in a directory use the following:

javac *.java // compliles all java files in the dir
java MyClass // runs the particular file

There are also a bunch of other related questions that should help you resolve this:

  1. How to run a java program from the command line
  2. How do I run java program with multiple classes from cmd?
  3. Problems running a java program from the command line interface
  4. Can't run multiple-class program from command line using packages

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.