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:
You have to specify the fully qualified class name (that means including the package name) to the
java
command. It looks like yourmyjava
class is in a packagecom.myexample.test
. So its fully qualified name iscom.myexample.test.myjava
.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).You're using the
src
directory, which contains.java
source files, but thejava
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 insidesrc
, 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
Java: How to Call Non Static Method from Main Method
How to Properly Shutdown Java Executorservice
What's the Right Way to Represent Phone Numbers
Getting Noclassdeffounderror When Using Common.Lang.Stringutils in Android Java Code
Listview Is Blank While Using Getfilter Function
Android: How to Stretch an Image to the Screen Width While Maintaining Aspect Ratio
How to Find the Index of an Element in an Array in Java
Where's My Invalid Character (Ora-00911)
What Do I Use Now That Handler() Is Deprecated
Return a Value from Asynctask in Android
Transitive Dependencies Not Resolved for Aar Library Using Gradle
Generic Type Inference Not Working with Method Chaining
How to Read Jsch Command Output
What Is the Syntax of the Enhanced for Loop in Java
How to Resolve Host "<Url Here>" No Address Associated with Host Name