我正在尝试从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是一次编写,随处运行)?


当前回答

你可以在命令行中使用javac编译任何java源代码;例如,javac CopyFile.java。 执行命令:java CopyFile。 你也可以使用javac *.java编译所有的java文件,只要它们在同一个目录下

如果你有一个问题导致“无法找到或加载主类”,你可能没有 Jre在你的路径。看看这个问题: 无法找到或加载主类

其他回答

从Java 9开始,JDK包含了jshell,一个Java REPL。

假设JDK 9+ bin目录被正确地添加到您的路径中,您将能够简单地:

运行jshell file .java - file .java当然是你的文件。 将打开一个提示符,允许您调用main方法:jshell> File.main(null)。 要关闭提示符并结束JVM会话,请使用/exit

JShell的完整文档可以在这里找到。

这很简单。如果你把文件保存为A.text,你应该做的第一件事就是保存为A.java。现在它是一个Java文件。

现在你需要打开cmd并在编译之前设置路径到你的A.java文件。你可以参考这个。

然后可以使用命令编译文件

javac A.java

然后使用

java的一个

这就是在cmd中编译和运行java程序的方法。 你也可以通过这些材料来深入学习Java。在Java中你需要理解的很多东西都涵盖在初学者那里。

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

javac filename.java

执行生成的类:

java filename

在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 11开始,Java命令行工具已经能够直接运行单文件源代码。如。

java HelloWorld.java

这是JEP 330的一个增强:https://openjdk.java.net/jeps/330

有关使用和限制的详细信息,请参阅Java实现的手册,例如Oracle提供的手册:https://docs.oracle.com/en/java/javase/11/tools/java.html