Home > Enterprise >  Multiples classes in java
Multiples classes in java

Time:06-02

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?

CodePudding user response:

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
  • Related