-2

I have a complete Java program, but the way that my professor is going to check to make sure that the program is working correctly is through CMD using the command
"java Caesar input.txt output.txt"

But the issues I am running into are that when I compile using "javac Caesar.java" it will compile and create a Caesar.class file in the folder, but when I try run it as "java caesar input.txt output.txt" it says Error: Could not find or load main class caesar.

What am I doing wrong here?
I can provide additional information as needed.

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

}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Ronave4C
  • 9
  • 1
  • 5
  • 3
    A capital C is different from a lower case c. Did you try both? – Dawood ibn Kareem Dec 04 '17 at 05:01
  • All Java identifiers are case-sensitive. `Caesar` and `caesar` are not the same identifier. – Andreas Dec 04 '17 at 05:03
  • Right, sorry that was a typo, and using both uppercase and lowercase provide the same error. Here's a screenshot of exactly what's going on: https://gyazo.com/a39a5793fced9fc67b22abb22317957c – Ronave4C Dec 04 '17 at 05:08
  • OK, so did a file called `Caesar.class` get created when you ran `javac`? And does your `Caesar` class inside `Caesar.java` contain any kind of package declaration? How about a method that starts with `public static void main(String[] ` ? – Dawood ibn Kareem Dec 04 '17 at 05:12
  • Yes a Caesar.class was created: https://gyazo.com/7b9b29aa825d0573dc952501efb8c18e And here is a snip of the code, displaying the main class: https://gyazo.com/d2e20b32fe2c8d72b9addac5d36575e6 After reading that commend i did try "java main decode.txt decoded.txt" which, again, yielded the same error. – Ronave4C Dec 04 '17 at 05:17
  • If this is what you mean, these are source packages in NetBeans: https://gyazo.com/513bbad9402fed458fefd2e6eafcd1e4 – Ronave4C Dec 04 '17 at 05:18
  • 1
    post your full code here – Mostch Romi Dec 04 '17 at 05:18
  • 1
    Is there a package declaration at the top of Caesar.java? – Dawood ibn Kareem Dec 04 '17 at 05:19
  • You're using NetBeans... Rum and compile there. Not the CMD – OneCricketeer Dec 04 '17 at 05:21
  • The full code was edited into the original post – Ronave4C Dec 04 '17 at 05:22
  • So you've got `Caesar.java` in one folder, and `Caaesar.java` in another. Looks like you've managed to confuse yourself about which one you're actually compiling. – Dawood ibn Kareem Dec 04 '17 at 05:24
  • Lol no sorry, they both contain the exact same code and are in virtually the same location, I made the other class to make sure it wasn't a fluke... – Ronave4C Dec 04 '17 at 05:28

2 Answers2

0

First of all javac Caesar.java should say file not found...

Your Caaesar class is part of the caaesar package.

The fully qualified class name is caaesar.Caaesar, which is how you run this class

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
0

If your professor intends to run

java Caesar input.txt output.txt

then you need all of the following. Spelling is important, and so is whether each letter is upper or lower case.

  • Your code file must be called Caesar.java,
  • The class in your code file must be called Caesar,
  • The class in your code file must contain a method that starts with public static void main(String[].
  • There must be no package declaration at the top of the file.

Please try to meet all these conditions, and be fastidious about the details.

Dawood ibn Kareem
  • 77,785
  • 15
  • 98
  • 110