How to Execute a Java .Class from the Command Line

How to execute a java .class from the command line

Try:

java -cp . Echo "hello"

Assuming that you compiled with:

javac Echo.java 

Then there is a chance that the "current" directory is not in your classpath ( where java looks for .class definitions )

If that's the case and listing the contents of your dir displays:

Echo.java
Echo.class

Then any of this may work:

java -cp . Echo "hello"

or

SET CLASSPATH=%CLASSPATH;.  

java Echo "hello"

And later as Fredrik points out you'll get another error message like.

Exception in thread "main" java.lang.NoSuchMethodError: main

When that happens, go and read his answer :)

How do I run a Java program from the command line on Windows?

Source: javaindos.

Let's say your file is in C:\mywork\

Run Command Prompt

C:\> cd \mywork

This makes C:\mywork the current directory.

C:\mywork> dir

This displays the directory contents. You should see
filenamehere.java among the files.

C:\mywork> set path=%path%;C:\Program Files\Java\jdk1.5.0_09\bin

This tells the system where to find JDK programs.

C:\mywork> javac filenamehere.java

This runs javac.exe, the compiler. You should see nothing but the
next system prompt...

C:\mywork> dir

javac has created the filenamehere.class file. You should see
filenamehere.java and filenamehere.class among the files.

C:\mywork> java filenamehere

This runs the Java interpreter. You should then see your program
output.

If the system cannot find javac, check the set path command. If javac
runs but you get errors, check your Java text. If the program
compiles but you get an exception, check the spelling and
capitalization in the file name and the class name and the java
HelloWorld command. Java is case-sensitive!

How to run java class command line?

You should execute a class using its full name. The full name of a class consists of:

<package location> + <simple name of class>

With this in mind, MainClass full name is com.company.application.MainClass.

Java can locate this class from the directory that contains the top directory in the package tree. From the example, the root directory is /apps:

apps <-- root
+ com <-- here the package starts
+ company
+ application
- MainClass.class

So this root directory should be in the classpath in order that Java program (the JVM) can access to the compiled classes and the entry point of the app.


From the question:

Assume the CLASSPATH environment variable is set to "." (current directory).

Option B states:

B. java com.company.application.MainClass if run from the /apps directory

Since you're in /apps directory and the current directory is the CLASSPATH, so Java can locate the designated class to execute.

Option C states:

C. java -classpath /apps com.company.application.MainClass if run from any directory

In this case, you're setting /apps as CLASSPATH, so Java can locate the designated class to execute.

All the other options fails to accomplish the explanation above.

How to execute a java class file from maven command line

It's -Dexec.mainClass instead of -Dexec:mainClass (dot instead of a colon).

See the usage page.

Run java class by command line

You have to change the directory to your src directory and than call `javac -cp ./ Project.LoginFrame.java

How do I compile my main Java program on the command line when it includes an external java package?

Your mistake is here

import org.somepackage; <--

public class MyProgram {

public static void main(String arguments[]) {

System.out.println("Programme up and running...");
Human myHuman = new Human();
myHuman.scream();

}

you forgot to import class actually, you need to write this name
import org.somepackage.Human; import all package content import org.somepackage.*; or write full qualified name of class in your code

org.somepackage.Human myHuman = new org.somepackage.Human(); 
myHuman.scream();

correct mistake:

import org.somepackage.Human;

public class MyProgram {

public static void main(String arguments[]) {

System.out.println("Programme up and running...");
Human myHuman = new Human();
myHuman.scream();

}

after that compile your Human.java by this command:

javac -d classes Human.java

and MyProgram.java

javac -d classes -cp "classes" MyProgram.java

and run MyProgram by

java -cp "classes" MyProgram

Execute Java Application from Command Line without package or class specification?

When you execute java <filename> it tries to find the file in the current directory. If it's not found, it returns an error saying could not find or load main class.

So, in your case, you are either executing java command from different directory or you haven't compiles the class. For the former, you can specify the class file path with -cp argument to command (e.g. java -cp FULLPATH CopyFile), for the latter case, you need to compile the class with javac first and then, execute it.

How do I run .class files on windows from command line?

The Java application launcher (a.k.a java.exe or simply java) supports up to four different ways to specify what to launch (depending on which Java version you use).

  1. Specifying a class name is the most basic way. Note that the class name is different from the file name.

     java -cp path/to/classFiles/ mypackage.Main

    Here we start the class mypackage.Main and use the -cp switch to specify the classpath which is used to find the class (the full path to the class mypackage.Main will be path/to/classFiles/mypackage/Main.class.

  2. Starting a jar file.

    java -jar myJar.jar

    This puts the jar itself and anything specified on its Class-Path entry on the class path and starts the class indicated via the Main-Class entry. Note that in this case you can not specify any additional class path entries (they will be silently ignored).

  3. Java 9 introduced modules and with that it introduce a way to launch a specific module in a way similar to how option #2 works (either by starting that modules dedicated main class or by starting a user-specified class within that module):

    java --module my.module
  4. Java 11 introduces support for Single-File Source Code Programs, which makes it very easy to execute Java programs that fit into a single source file. It even does the compile step for you:

    java MyMain.java

    This option can be useful for experimenting with Java for the first time, but quickly reaches its limits as it will not allow you to access classes that are defined in another source file (unless you compile them separately and put them on the classpath, which defeats the ease of use of this method and means you should probably switch back to option #1 in that case).

    This feature was developed as JEP 330 and is still sometimes referred to as such.

For your specific case you'd use option #1 and tell java where to look for that class by using the -classpath option (or its short form -cp):

java -classpath C:\Peter\Michael\Lazarus\ Main

If your Main.java contains the entirety of your source code (and it is in the same directory), then you can use option #4, skip the compile step and directly compile-and-execute it:

java c:\Peter\Michael\Lazarus\Main.java

Run a Java Class with Command-Line

Update

From your comment, I learnt that you have package ValueInput; mentioned in ExecutionEngine.java. Therefore, you should use the switch -d when compiling:

javac -d . -cp "C:\Users\ABC\selenium-java-2.48.2\selenium-2.48.2\selenium-java-2.48.2.jar;selenium-server-standalone-3.141.59.j‌​ar" *.java

The option -d . asks the compiler to place the generated class files at the current directory. Now, if you use the command ls in Mac/Unix or dir in Windows, you will see a directory, ValueInput has been created and all the .class files have been placed inside this directory. Learn more about the switches by simply using the command javac

In order to execute ExecutionEngine.class, you can now use the following command:

java -cp ".;C:\Users\ABC\selenium-java-2.48.2\selenium-2.48.2\selenium-java-2.48.2.jar;selenium-server-standalone-3.141.59.j‌​ar" ValueInput.ExecutionEngine 

You can also check this answer for a similar solution.

Side note: You should follow the Java naming conventions. As per the convention, the name of the package should be something like value.input.

Original answer

The root cause of the problem is using only jars with -cp. You missed realizing that your ExecutionEngine.class is not in the jars; rather it is at the current directory which is denoted by a dot (.) which you missed to include in the classpath.

Thus, the correct command will be:

java -cp ".;C:\Users\ABC\selenium-java-2.48.2\selenium-2.48.2\selenium-java-2.48.2.jar;selenium-server-standalone-3.141.59.j‌​ar" ExecutionEngine 

It doesn't matter where you put . i.e. the current directory e.g. the following will also work for you:

java -cp "C:\Users\ABC\selenium-java-2.48.2\selenium-2.48.2\selenium-java-2.48.2.jar;selenium-server-standalone-3.141.59.j‌​ar;." ExecutionEngine 

Note for Mac:

The separator used for this purpose in Mac is : instead of ; e.g.

javac -cp mysql-connector-java-5.1.49.jar MysqlDemo.java
java -cp mysql-connector-java-5.1.49.jar:. MysqlDemo

Note for Java-11 onwards:
Java-11 allows launching Single-File Source-Code programs without compiling e.g.

java -cp mysql-connector-java-5.1.49.jar MysqlDemo.java

You can learn more about it from this article.

How to run Java program in command prompt

javac is the Java compiler. java is the JVM and what you use to execute a Java program. You do not execute .java files, they are just source files.
Presumably there is .jar somewhere (or a directory containing .class files) that is the product of building it in Eclipse:


java/src/com/mypackage/Main.java
java/classes/com/mypackage/Main.class
java/lib/mypackage.jar

From directory java execute:

java -cp lib/mypackage.jar Main arg1 arg2



Related Topics



Leave a reply



Submit