运行Java程序时命令行出错

时间:2017-12-04 04:59:42

标签: java command-line javac

我有一个完整的Java程序,但我的教授要检查以确保程序正常工作的方式是通过CMD使用命令
 "java Caesar input.txt output.txt"

但我遇到的问题是,当我使用" javac Caesar.java"它将在文件夹中编译并创建一个Caesar.class文件,但当我尝试将其作为" java caesar input.txt output.txt"它说错误:Could not find or load main class caesar

我在这里做错了什么?
我可以根据需要提供其他信息。

package caaesar;

import java.io.File;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.util.Scanner;

/**
 *
 * @author Connorbboggs
 * Version 12.03.2017
 */
public class Caaesar 
{

private static Scanner sc;
private static FileWriter fw;
private static BufferedWriter bw;
private Scanner inp;
private Scanner outp;
//DecodeChar class provided by you
public static char decodeChar(int n, char ch)
{
    int ord;
    if (ch >= 'A' && ch <= 'Z')
    {
        ord = ch - 'A';
        ord += n;
        ord %= 26;
        return (char)(ord + 'A');
    }
    else if (ch >= 'a' && ch <= 'z')
    {
        ord = ch - 'a';
        ord += n;
        ord %= 26;
        return (char)(ord + 'a');
    }
    else
    {
        return ch;
    }
}

public static void main(String[] args)
{
    //File input for decode.txt with the encrypted text
    File inputFile = new File(args[0]);
    //Output for the decrypted text
    File outputFile = new File(args[1]);
    try 
    {
        //input from inputFile
        sc = new Scanner(inputFile);
        int shift = sc.nextInt();
        String decoded = "";
        //while lines are available text is fed to inputFile
        while( sc.hasNextLine())
        {
            //Lines being fed into string line
            String line = sc.nextLine();
            //for loop to feed into decoded using decodeChar
            for(int i=0; i<line.length(); i++)
            {
                decoded += decodeChar(shift, line.charAt(i));                    
            }
            decoded += '\n';
        }
        //Just to verify that the text is decrypted
        System.out.println(decoded);
        try
        {
            //create a fileWriter for outputFile
            fw = new FileWriter(outputFile);
            //write "decoded" to the file
            fw.write(decoded);
            //close the file
            fw.close();
        }
        //exception catching
        catch (IOException ex)
        {
            ex.printStackTrace();
        }
    }
    //more exception 
    catch (FileNotFoundException e) 
    {
        e.printStackTrace();
    }
}

}

2 个答案:

答案 0 :(得分:0)

首先javac Caesar.java应该说文件未找到...

您的 Caaesar 课程是 caaesar 课程的一部分。

完全限定的类名是caaesar.Caaesar,这就是你运行这个类的方法

答案 1 :(得分:0)

如果您的教授打算运行

java Caesar input.txt output.txt

然后您需要以下所有内容。拼写很重要,每个字母是大写还是小写也是如此。

  • 您的代码文件必须名为Caesar.java
  • 必须将代码文件中的类称为Caesar
  • 代码文件中的类必须包含以public static void main(String[]开头的方法。
  • 文件顶部必须有 no 包声明。

请尽量满足所有这些条件,并对细节保持谨慎。

相关问题