-1

I'm new in Java, need to compile 4 files in the same directory, for this, in my command line, I typed:

javac Bag.java Graph.java DirectedDFS.java principal.java

The code compile, then I try to run my file, call tinyG.txt, in class principal.java, for this, i run in my command line:

java principal < tinyG.txt

but appear the error:

java.lang.NoClassDefFoundError: graph/principal (wrong name: principal)

graph it's my package. My biggest question is how the class principal will read the .txt file

This is my principal class:

package graph;

import java.util.Scanner;

import graph.Bag;
import graph.Graph;
import graph.DirectedDFS;

public class principal {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner in = new Scanner(System.in);
        Graph G = new Graph(in);

        // read in sources from command-line arguments
        Bag<Integer> sources = new Bag<Integer>();
        while (in.hasNext()) {
            int s = in.nextInt();
            sources.add(s);
        }
        
        in.close();

        DirectedDFS dfs = new DirectedDFS(G, sources);

        // print out vertices reachable from sources
        for (int v = 0; v < G.V(); v++) {
            if (dfs.marked(v)) System.out.print(v + " ");
        }
        System.out.println();
    }
}

How i fix this?

Marcelo de Sousa
  • 205
  • 2
  • 12
  • 1
    Does this answer your question? [How do I run a Java class in a package?](https://stackoverflow.com/questions/11032590/how-do-i-run-a-java-class-in-a-package) – Federico klez Culloca Jun 01 '22 at 12:43
  • I had already seen this question, my biggest question is how the class principal will read the .txt file – Marcelo de Sousa Jun 01 '22 at 12:49
  • I'm not sure I understand what you mean by this last comment (meaning, how it relates with the problem at hand) – Federico klez Culloca Jun 01 '22 at 12:50
  • 3
    Your error has nothing to do with reading the text file, as your program is not even reaching that point. The program is failing to even launch because the main class you specified is not being found. Fix that before worrying about reading the text file. – Slaw Jun 01 '22 at 12:52
  • As an aside, using `< file.txt` redirects the standard input stream to that file. So, you would read that text file via `System.in`. If you meant to pass the path to the file as an _argument_ then get rid of the `<`. Though note the `Scanner(String)` constructor scans the given `String`, it does not interpret the string as a file path and open a file (there are other constructors that accept files/paths). – Slaw Jun 01 '22 at 13:08
  • @Slaw I changed it as per your comment. Is it correct now? – Marcelo de Sousa Jun 01 '22 at 13:15
  • Possibly. But you still have your original problem, which seems similar to [How to fix NoClassDefFoundError?](https://stackoverflow.com/q/67808023/6395627). – Slaw Jun 01 '22 at 13:21
  • @slaw I'm new to Java, and I tried to follow the instructions, but honestly I couldn't generalize the solution to multiple files. – Marcelo de Sousa Jun 01 '22 at 13:38
  • 1
    In your case, try `javac -d . Bag.java Graph.java DirectedDFS.java principal.java` followed by `java graph.principle < tinyG.txt`. – Slaw Jun 01 '22 at 15:58
  • 1
    And I recommend you research how to organize Java source files (i.e., create a directory structure that mirrors the package structure). I also recommend you stop trying to compile and execute Java on the command line and instead use an IDE (e.g., IntelliJ, Eclipse, NetBeans, etc.) to do the same (or even a build tool such as Maven or Gradle, though those are more advanced topics). – Slaw Jun 01 '22 at 15:59

1 Answers1

2

As it was mentioned in comments < redirects the standard input stream to file. So you should use

Scanner in = new Scanner(System.in);

and also to run program from command line you should be in folder that contains folder named graph and you need to specify package:

java graph.principal < tinyG.txt