Running Java Program from Command Line Linux

Running Java Program from Command Line Linux

If your Main class is in a package called FileManagement, then try:

java -cp . FileManagement.Main

in the parent folder of the FileManagement folder.

If your Main class is not in a package (the default package) then cd to the FileManagement folder and try:

java -cp . Main

More info about the CLASSPATH and how the JRE find classes:

  • How Classes are Found
  • Setting the class path (Solaris/Linux)
  • http://en.wikipedia.org/wiki/Classpath_(Java)

Compile and build with single command line Java (Linux)

An alias is not made to accept parameters, define a function like this:

jcar() { javac $1.java && java $1 ; }

Then use it:

jcar Program

(jcar was intended as an acronym for java-compile-and-run)

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!

Running java in package from command line

You'd run it as:

java One.Test

... but from the root directory (basic), not from the One directory. You always specify the fully-qualified class name.

Oh, and package names in Java should be lower-case, so it should be one and one.inner, not One and One.Inner. Just a convention, but one which pretty much everyone follows.

Java app from Linux command line stops on exit

You can use nohup command.

nohup java -jar app.jar &

Incase you need to see output/errors of your program then you can look into nohup.out file in current directory. Standard output(eg. whatever you print using System.out.println()) and error messages will go to this file.

For more details : run-bash-commands-background-linux

How do i run a Linux terminal cmd from a java program

You can use the below command format to run your Linux command.

Runtime r = Runtime.getRuntime();
Process p = r.exec(yourcmd);

Please go through Running unix command from Java and Unable to run Unix command in Java-Stackoverflow

Hope you get your answers here.

Is it possible to run Java program from command line without compiling?

You ask:

Isn't this skipping the compile process?

Absolutely yes. A command line like java JavaClassName 4 7 file.csv assumes that there is a compiled class file "JavaClassName.class" in the current directory (or in some other directory or Zip/Jar file found in the CLASSPATH environment variable). And yes, to produce that "JavaClassName.class" class file, you have to use a java compiler first.

How to run java program in command prompt,created by intellij

Three issues:

  1. You have to specify the fully qualified class name (that means including the package name) to the java command. It looks like your myjava class is in a package com.myexample.test. So its fully qualified name is com.myexample.test.myjava.

  2. When you run the java command you have to be in the directory that is at the base of the package hierarchy (or put that directory on the classpath).

  3. You're using the src directory, which contains .java source files, but the java command expects compiled .class files, so you need to use the project's output directory. Its location in your project will depend on your IDE and configuration but it will contain same-named structure as inside src, except with .class files instead of .java files.

In your case, navigate to:

C:\myjava\sampl1\out\production\

Then run:

java com.myexample.test.myjava


Related Topics



Leave a reply



Submit