Nerotix
Nerotix

Reputation: 375

Error: Could not find or load main class - path is correct

As you can see in the title, I'm getting the error:

Error: Could not find or load main class Exercise14_11.java

I checked my path and it should be working, when I type java -version in CMD I get the proper output. Which should indicate the system path is set correctly.

I'm opening the CMD from the same folder as where the Exercise14_11.java is located.

But when I typt: java Exercise14_11.java

I get the error:

Error: Could not find or load main class Exercise14_11.java

I've read the answers that has been asked for this error before but it has not really helped.

Hope someone can tell me whats wrong.

PS Here's the program I'm trying to run:

package hsleiden.webcat.exercise14_11;

import java.io.*;
import java.util.*;

public class Exercise14_11 {
    public static void main(String[] args) throws Exception {
        if (args.length != 2){
            System.out.println("Usage: java Exercise14_11 stringTeVerwijderen sourceFile");
        }

        File sourceFile = new File(args[1]);
        if(!sourceFile.exists()){
            System.out.println("Source file " + args[1] + " does not exist");
        }

        Scanner input = new Scanner(sourceFile);
        StringBuilder sb = new StringBuilder();

        while(input.hasNext()){
            String watVervangen = input.nextLine();
            String vervangen = watVervangen.replaceAll(args[0], "");
            sb.append("\r\n" + vervangen);
        }

        input.close();

        PrintWriter output = new PrintWriter(sourceFile);
        output.println(sb.toString());
        output.close();
    }
}

Upvotes: 0

Views: 1239

Answers (2)

Mario Rossi
Mario Rossi

Reputation: 7799

The origin of the problem seems to be that your class belongs to package hsleiden.webcat.exercise14_11, and you are working as it belonged to the default package. If you sucessfully compiled it, the .class file should be inside a directory C:\...\DDD\hsleiden\webcat\exercise14_11, and be named Exercise14_11.class. To run it, either

  1. Add directory C:\...\DDD to the classpath, or
  2. Add directory . to the classpath and cd to C:\...\DDD.

Additionally, the java command requires the full class name, so you should use:

java hsleiden.webcat.exercise14_11.Exercise14_11

Please note that when compiling it's correct to use the .java filename extension, but when running it's necessary to use a class name. These don't have a filename extension, so don't add .java or .class.

For example, if the .class file full path were C:\eclipse\IOPR2\Exercise14_11\bin\hsleiden\webcat\exercise14_11\Exercise14_11.class, then you would need to run it with:

java  -cp C:\eclipse\IOPR2\Exercise14_11\bin  hsleiden.webcat.exercise14_11.Exercise14_11

Upvotes: 2

Crazyjavahacking
Crazyjavahacking

Reputation: 9697

You have to compile the class, and then run it. You have wrong command:

javac Exercise14_11.java
java  Exercise14_11
  • javac command takes the source files and produces the compiled classes
  • java command takes the compiled class with main method to execute

Upvotes: 1

Related Questions