如何在 Windows 上从命令行运行 Java 程序? [英] How do I run a Java program from the command line on Windows?

查看:33
本文介绍了如何在 Windows 上从命令行运行 Java 程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

推荐答案

来源:javaindos.

假设您的文件在 C:\mywork\

Let's say your file is in C:\mywork\

运行命令提示符

C:\> cd \mywork

这使 C:\mywork 成为当前目录.

This makes C:\mywork the current directory.

C:\mywork> dir

这将显示目录内容.你应该看到文件中的filenamehere.java.

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

这告诉系统在哪里可以找到 JDK 程序.

This tells the system where to find JDK programs.

C:\mywork> javac filenamehere.java

这会运行编译器 javac.exe.你应该只看到下一个系统提示...

This runs javac.exe, the compiler. You should see nothing but the next system prompt...

C:\mywork> dir

javac 已经创建了 filenamehere.class 文件.你应该看到文件中的filenamehere.java 和filenamehere.class.

javac has created the filenamehere.class file. You should see filenamehere.java and filenamehere.class among the files.

C:\mywork> java filenamehere

这将运行 Java 解释器.然后你应该看到你的程序输出.

This runs the Java interpreter. You should then see your program output.

如果系统找不到javac,检查set path命令.如果javac运行但出现错误,请检查您的 Java 文本.如果程序编译但你得到一个异常,检查拼写和文件名、类名和 java 中的大写HelloWorld 命令.Java 区分大小写!

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!

这篇关于如何在 Windows 上从命令行运行 Java 程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆