0

I have this in a terminal. When I call this from the home directory there is no problems it works perfectly fine. But when I put this into a sub directory CPS3498 and compile it there is no error. When I try to do java home/student/pshivam/CPS3498/encrypt to run the program it gives me this error

Exception in thread "main" java.lang.NoClassDefFoundError: /home/student/pshivam/CPS3498/encrypt
Caused by: java.lang.ClassNotFoundException: .home.student.pshivam.CPS3498.encrypt
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
Could not find the main class: /home/student/pshivam/CPS3498/encrypt.  Program will exit

Code as follows.

import java.lang.ClassNotFoundException;
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;

    public class encrypt {

        public static void main(String[] args) throws IOException {
            try {
                File file = new File("/home/student/pshivam/CPS3498/letter.txt");
                FileReader fileReader = new FileReader(file);
                BufferedReader bufferedReader = new BufferedReader(fileReader);
                StringBuffer stringBuffer = new StringBuffer();
                String line;
                while ((line = bufferedReader.readLine()) != null) {
                    stringBuffer.append(line);
                    stringBuffer.append("\n");
                }
                fileReader.close();
                for(int i = 0; i<stringBuffer.length(); i++){
                    int encrypt = 0;
                    encrypt = (int)stringBuffer.charAt(i);
                    encrypt = encrypt * 9;
                    stringBuffer.setCharAt(i, (char)encrypt);
                    File message = new File("/home/student/pshivam/CPS3498/secret.txt");
                    try
                    {
                        message.createNewFile();
                        FileWriter msgFile = new FileWriter(message);
                        BufferedWriter msgBuff = new BufferedWriter(msgFile);
                        msgBuff.write(stringBuffer.toString());
                        msgBuff.close();
                    } 
                    finally 
                    {

                    }
                }

            } finally{

            }


        }
    }
njzk2
  • 38,969
  • 7
  • 69
  • 107
Shiv
  • 1
  • 2

1 Answers1

0

If you don't have a package specified for the encrypt class, then you should be able to run it from any directory using java encrypt from the directory where the .class file is located.

If you had specified the following package:

package home.student.pshivam.CPS3498

then you would build from the root directory above home (presumably \ if you are using Linux). You would then run the program again from the root directory using

java home.student.pshivam.CPS3498.encrypt

By the way, naming convention in Java is that class names should begin with a captial letter, i.e. use Encrypt rather than encrypt.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360