-1

Please, help me to find a mistake. I have a class file. I take a byte code of this class and encrypt it by Caesar cipher. I have several arguments of command prompt:

  1. class file: which we encrypt,
  2. class file: in which the output will transmit
  3. key: the number which we add to every number of byte code of the first argument.

I want simply to encrypt the first argument with a zero key, output will transmit to second argument, and then I want to run a second file (second argument).

I compile:

javac Caesar.java

Run:

java Caesar HelloWorld.class Hello1.class 0

(attention - I use zero key, so, file shouldn't be changed)

And run the second file, And I see such mistake:

julia@julia-Aspire-5680 ~/zagruzchik $ java Hello1 Exception in thread "main" java.lang.NoClassDefFoundError: Hello1 (wrong name: HelloWorld) at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClass(ClassLoader.java:800) at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142) at java.net.URLClassLoader.defineClass(URLClassLoader.java:449) at java.net.URLClassLoader.access$100(URLClassLoader.java:71) at java.net.URLClassLoader$1.run(URLClassLoader.java:361) at java.net.URLClassLoader$1.run(URLClassLoader.java:355) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:354) at java.lang.ClassLoader.loadClass(ClassLoader.java:425) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308) at java.lang.ClassLoader.loadClass(ClassLoader.java:358) at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:482)

My source code:

import java.io.*;
import static java.lang.System.out;

/**
encript by caesar chipher
*/
public class Caesar {
    public static void main(String[]args) {
        if (args.length == 3)
        {
        }
        else {
            System.out.println("USAGE: java Caesar in out key");
            return;
        }
        try ( //class which will be ciphered
            FileInputStream in = new FileInputStream(args[0])) {
                //in output will go encripted byte code of file
                FileOutputStream out = new FileOutputStream(args[1]);
                //this is key
                int key = Integer.parseInt(args[2]);
                int ch;

                //in cycle encript byte code
                while ((ch = in.read()) != -1) {
                    byte c = (byte)(ch + key);
                    out.write(c);
                }
            }
        catch(IOException exception)
        {
        }
        out.close();
    }
}
Draken
  • 3,134
  • 13
  • 34
  • 54
  • Your code is currently incorrect with regards to your bracket usage. I've tried to format it, but this line was causing an issue and I wasn't sure where you're `{` was supposed to go: `try ( //class which will be ciphered` – Draken Apr 22 '16 at 06:29

1 Answers1

0

Beside the way you want to encrypt the file there is a more fundamental issue.

I want simply to encrypt the first argument with a zero key, output will transmit to second argument, and then I want to run a second file (second argument).

which manifests itself in this exception

$ java Hello1 
Exception in thread "main"
java.lang.NoClassDefFoundError: Hello1 (wrong name: HelloWorld) 

A class name must be equal to the *.class file name. If your class name is HelloWorld it can only be used if it is stored in a class file with the name HelloWorld.class.

If you rename the file to Hello1.class it will not work, as you instruct the Java Virtual Machine to execute class Hello1 but this class is not stored the file Hello1.class.

SubOptimal
  • 22,518
  • 3
  • 53
  • 69