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


当前回答

您实际上可以像运行shell或python脚本一样运行Java程序,而无需手动编译Java文件,如中所述 中330。它从JDK 11开始提供。

如果您创建了一个测试文件,并放入以下内容,您应该能够将其作为命令测试运行。您需要使用chmod +x测试使其在Linux和Mac OSX中可执行。

    #!/usr/bin/env java --source 11
    public class Test {
        public static void main(String [] args) {
            System.out.println("Hello world!");
            System.exit(0);
        }
    }

在前面的例子中,不允许使用.java文件扩展名。

    $ chmod +x testing
    $ ./testing
    Hello world!
    $

但是如果它的名称是Test.java,没有shebang“#!”前缀,你仍然可以执行,就像这样:

    public class Test {
        public static void main(String [] args) {
            System.out.println("Hello again!");
            System.exit(0);
        }
    }

然后使用以下命令执行Test.java文件:

    $ java Test.java
    Hello again!
    $

所以只要你在路径中有一个新的Java编译器,这就可以工作,用Java -version检查。更多信息请见本博客。

其他回答

来源: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!

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

     javac Student.java
     java Student

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

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

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

javac A.java

然后使用

java的一个

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

完整回答:

The Java File TheJavaFile.java Compile the Java File to a *.class file javac TheJavaFile.java This will create a TheJavaFile.class file Execution 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

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

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

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

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