Reputation: 2871
I'm trying to execute a Java program from the command line in Windows.
Here is my code:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class CopyFile {
public static void main(String[] args) {
InputStream inStream = null;
OutputStream outStream = null;
try {
File afile = new File("input.txt");
File bfile = new File("inputCopy.txt");
inStream = new FileInputStream(afile);
outStream = new FileOutputStream(bfile);
byte[] buffer = new byte[1024];
int length;
// copy the file content in bytes
while ((length = inStream.read(buffer)) > 0) {
outStream.write(buffer, 0, length);
}
inStream.close();
outStream.close();
System.out.println("File is copied successful!");
} catch (IOException e) {
e.printStackTrace();
}
}
}
I'm not sure how to execute the program - any help?
Is this possible on Windows?
Why is it different than another environment (I thought JVM was write once, run anywhere)?
Upvotes: 271
Views: 1648232
Reputation: 542
You can actually run Java program as you would shell or python scripts without manually compile the Java file, as described in JEP 330. That is available since JDK 11.
If you create a file testing
, and put the following you should be able to run it as command testing
. You need to make it executable in Linux and Mac OSX with chmod +x testing
. You might need to add the switch -S
after env
to split the arguments to java
first. You will notice this if you get an error about java --source 11
doesn't exists.
#!/usr/bin/env java --source 11
public class Test {
public static void main(String [] args) {
System.out.println("Hello world!");
System.exit(0);
}
}
You are not allowed to use the file extension .java
in the previous example.
$ chmod +x testing
$ ./testing
Hello world!
$
But you can still execute if it is was name Test.java
without the shebang "#!" prefix like this:
public class Test {
public static void main(String [] args) {
System.out.println("Hello again!");
System.exit(0);
}
}
Then execute the file Test.java
with following command:
$ java Test.java
Hello again!
$
So this works as long as you have a new enough Java compiler in the path, check with java -version
. More information in this blog.
Upvotes: 0
Reputation: 451
Since Java 11, java
command line tool has been able to run a single-file source-code directly. e.g.
java HelloWorld.java
This was an enhancement with JEP 330: https://openjdk.java.net/jeps/330
For the details of the usage and the limitations, see the manual of your Java implementation such as one provided by Oracle: https://docs.oracle.com/en/java/javase/11/tools/java.html
Upvotes: 30
Reputation: 44
Now (with JDK 9 onwards), you can just use java to get that executed. In order to execute "Hello.java" containing the main, one can use: java Hello.java
You do not need to compile using separately using javac anymore.
Upvotes: 2
Reputation: 171
As of Java 9, the JDK includes jshell
, a Java REPL.
Assuming the JDK 9+ bin
directory is correctly added to your path, you will be able to simply:
jshell File.java
— File.java
being your file of course. main
method: jshell> File.main(null)
./exit
Full documentation for JShell can be found here.
Upvotes: 2
Reputation: 541
In case your Java class is in some package. Suppose your Java class named ABC.java
is present in com.hello.programs
, then you need to run it with the package name.
Compile it in the usual way:
C:\SimpleJavaProject\src\com\hello\programs > javac ABC.java
But to run it, you need to give the package name and then your java class name:
C:\SimpleJavaProject\src > java com.hello.programs.ABC
Upvotes: 26
Reputation: 863
Complile a Java file to generate a class:
javac filename.java
Execute the generated class:
java filename
Upvotes: 26
Reputation: 2781
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!
Upvotes: 273
Reputation: 355
STEP 1: FIRST OPEN THE COMMAND PROMPT WHERE YOUR FILE IS LOCATED. (right click while pressing shift)
STEP 2: THEN USE THE FOLLOWING COMMANDS TO EXECUTE.
(lets say the file and class name to be executed is named as Student.java)The example program is in the picture background.
javac Student.java
java Student
Upvotes: 3
Reputation: 377
Assuming the file is called "CopyFile.java", do the following:
javac CopyFile.java
java -cp . CopyFile
The first line compiles the source code into executable byte code. The second line executes it, first adding the current directory to the class path (just in case).
Upvotes: 14
Reputation: 688
On Windows 7 I had to do the following:
quick way
long way
This likely breaks when you upgrade your JDK installation but you have access to all the command line tools now.
Follow comments above about how to compile the file ("javac MyFile.java" then "java MyFile") https://stackoverflow.com/a/33149828/194872
Upvotes: 4
Reputation: 141
It is easy. If you have saved your file as A.text first thing you should do is save it as A.java. Now it is a Java file.
Now you need to open cmd and set path to you A.java file before compile it. you can refer this for that.
Then you can compile your file using command
javac A.java
Then run it using
java A
So that is how you compile and run a java program in cmd. You can also go through these material that is Java in depth lessons. Lot of things you need to understand in Java is covered there for beginners.
Upvotes: 9
Reputation: 1717
To complete the answer :
The Java File
TheJavaFile.java
Compile the Java File to a *.class file
javac TheJavaFile.java
TheJavaFile.class
fileExecution of the Java File
java TheJavaFile
Creation of an executable *.jar
file
You've got two options here -
With an external manifest file :
Create the manifest file say - MANIFEST.mf
The MANIFEST file is nothing but an explicit entry of the Main Class
jar -cvfm TheJavaFile.jar MANIFEST.mf TheJavaFile.class
Executable by Entry Point:
jar -cvfe TheJavaFile.jar <MainClass> TheJavaFile.class
To run the Jar File
java -jar TheJavaFile.jar
Upvotes: 120
Reputation: 499
You can compile any java source using javac in command line ; eg, javac CopyFile.java. To run : java CopyFile. You can also compile all java files using javac *.java as long as they're in the same directory
If you're having an issue resulting with "could not find or load main class" you may not have jre in your path. Have a look at this question: Could not find or load main class
Upvotes: 8