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

后端 未结 12 1680
广开言路
广开言路 2020-11-22 03:03

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.         


        
相关标签:
12条回答
  • 2020-11-22 03:51

    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

    0 讨论(0)
  • 2020-11-22 03:52

    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:

    1. Run jshell File.javaFile.java being your file of course.
    2. A prompt will open, allowing you to call the main method: jshell> File.main(null).
    3. To close the prompt and end the JVM session, use /exit

    Full documentation for JShell can be found here.

    0 讨论(0)
  • 2020-11-22 03:53

    On Windows 7 I had to do the following:

    quick way

    1. Install JDK http://www.oracle.com/technetwork/java/javase/downloads
    2. in windows, browse into "C:\Program Files\Java\jdk1.8.0_91\bin" (or wherever the latest version of JDK is installed), hold down shift and right click on a blank area within the window and do "open command window here" and this will give you a command line and access to all the BIN tools. "javac" is not by default in the windows system PATH environment variable.
    3. Follow comments above about how to compile the file ("javac MyFile.java" then "java MyFile") https://stackoverflow.com/a/33149828/194872

    long way

    1. Install JDK http://www.oracle.com/technetwork/java/javase/downloads/index.html
    2. After installing, in edits the Windows PATH environment variable and adds the following to the path C:\ProgramData\Oracle\Java\javapath. Within this folder are symbolic links to a handful of java executables but "javac" is NOT one of them so when trying to run "javac" from Windows command line it throws an error.
    3. I edited the path: Control Panel -> System -> Advanced tab -> "Environment Variables..." button -> scroll down to "Path", highlight and edit -> replaced the "C:\ProgramData\Oracle\Java\javapath" with a direct path to the java BIN folder "C:\Program Files\Java\jdk1.8.0_91\bin".
    4. This likely breaks when you upgrade your JDK installation but you have access to all the command line tools now.

    5. Follow comments above about how to compile the file ("javac MyFile.java" then "java MyFile") https://stackoverflow.com/a/33149828/194872

    0 讨论(0)
  • 2020-11-22 03:55

    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
    
    0 讨论(0)
  • 2020-11-22 03:59

    Complile a Java file to generate a class:

    javac filename.java
    

    Execute the generated class:

    java filename
    
    0 讨论(0)
  • 2020-11-22 04:00

    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).

    0 讨论(0)
提交回复
热议问题