我正在尝试从Windows中的命令行执行Java程序。这是我的代码:

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();
        }
    }
}

我不知道如何执行这个程序——有什么帮助吗?这在Windows上可行吗?为什么它与其他环境不同(我以为JVM是一次编写,随处运行)?


当前回答

在Windows 7上,我必须执行以下操作:

快速的方法

Install JDK http://www.oracle.com/technetwork/java/javase/downloads 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. Follow comments above about how to compile the file ("javac MyFile.java" then "java MyFile") https://stackoverflow.com/a/33149828/194872

漫长的道路

Install JDK http://www.oracle.com/technetwork/java/javase/downloads/index.html 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. 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". 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

其他回答

万一你的Java类在某个包里。假设在com.hello中出现了名为ABC.java的Java类。程序,然后您需要使用包名运行它。

按通常的方式编译:

C:\SimpleJavaProject\src\com\hello\programs > javac ABC.java

但是要运行它,你需要给出包名,然后是你的java类名:

C:\SimpleJavaProject\src > java com.hello.programs.ABC

现在(从JDK 9开始),您可以使用java来执行该命令。 为了执行包含main的"Hello.java",可以使用: java Hello.java

你不再需要单独使用javac进行编译。

编译一个Java文件来生成一个类:

javac filename.java

执行生成的类:

java filename

假设文件名为“CopyFile.java”,执行以下操作:

javac CopyFile.java
java -cp . CopyFile

第一行将源代码编译为可执行的字节代码。第二行执行它,首先将当前目录添加到类路径(以防万一)。

第一步:首先打开文件所在的命令提示符。(按住shift键右键点击) 步骤2:然后使用以下命令执行。 (假设要执行的文件和类名为Student.java)示例程序在图片背景中。

     javac Student.java
     java Student