troubleshooting Question

"Cannot find main" error when attempting to run "java name.class" file

Avatar of Mike R.
Mike R. asked on
LinuxJava* commands
3 Comments1 Solution149 ViewsLast Modified:
I'm not a Java programmer, but my boss tasked me with getting someone else's code running. Sorry for the noob question.

I have the code running perfectly via `Eclipse` on my OSX system. However, the need is for it to be running from the command line.

I'm trying to run the code on my OSX box that was compiled by eclipse when I ran it there.

I've set up the environment as best I can. But when I attempt to run the *.class files, I get...

    init mikes$ pwd
    /Users/(snip)/Eclipseworkspace/Anonymize/bin/init

    init mikes$ java Init.class
    Error: Could not find or load main class Init.class

    init mikes$ java ./Init.class
    Error: Could not find or load main class ..Init.class

`main` is definitely in Init.class

**Init.java**

    package init;
   
    import java.io.File;
    import java.io.IOException;
    import java.nio.file.Files;
    import java.nio.file.Path;
    import java.nio.file.Paths;
    import java.util.stream.Stream;
    import java.net.*;
   
    import anonymizer.Anonymizer;
   
    public class Init  {
          
          public static String STOPWORDS;
          (...snip...)

          static {
                // Define location of resource files
                 STOPWORDS = "misc" + File.separator + "stopwordlist_de.txt";
            (...snip...)
          }
          
          public static void main(String[] args) throws IOException {            
                if (args.length !=1) {
                      System.err.println("Usage: Anonymize <path to docx files>");
                      System.exit(1);
                    }
                try (Stream<Path> paths = Files.walk(Paths.get(args[0]))) {
                      paths.skip(1).filter(file -> file.toString().endsWith("docx")).forEach(file -> {
                                  Anonymizer ano;
                            try {
                                  // Creates an instance of the Anonymizer class and executes the pipeline
                                  ano = new Anonymizer(file.toString(), file.toString()+".anon");
                                    ano.anonymizeAdmissionNotes();
                            } catch (Exception e) {
                                  e.printStackTrace();
                            }
                      });
                    } catch (IOException e) {
                      e.printStackTrace();
                    }
                
                System.out.println("De-Identification finished.");
   
          }
    }    

** Directory Structure **

    Anonymize mikes$ pwd
    /Users/(snip)/Eclipseworkspace/Anonymize/
   
    Anonymize mikes$ find . | grep -v <non_pertinent_stuff>
    .
    ./misc
    ./misc/suffixes_towns.txt
    ./misc/regex_mappings_surnames.txt
    ./misc/regex_mappings_international_firstnames.txt
    ./misc/suffixes_streets.txt
    ./misc/tokenizer.jar
    ./misc/stopwordlist_de.txt
    ./misc/regex_mappings_streets_with_numbers_and_abr.txt
    ./misc/header_tokens.txt
    ./misc/regex_mappings_general.txt
    ./misc/regex_mappings_towns_and_villages.txt

    ./bin
    ./bin/init
    ./bin/init/Init.class
    ./bin/anonymizer
    ./bin/anonymizer/DocxReader.class
    ./bin/anonymizer/Anonymizer.class
    ./bin/anonymizer/NamedEntityRecognizer.class
    ./.classpath

    ./lib
    ./lib/xmlbeans-2.6.0.jar
    ./lib/poi-3.17.jar
    ./lib/poi-ooxml-schemas-3.17.jar
    ./lib/poi-ooxml-3.17.jar
    ./lib/stanford-german-corenlp-2017-06-09-models.jar
    ./lib/stanford-corenlp-3.8.0.jar

** This is how I tried to setup the environment, and run the code. **

    init mikes$ export CLASSPATH="${CLASSPATH}:.:\
    /Users/(snip)/Eclipseworkspace/Anonymize/bin/:\
    /Users/(snip)/Eclipseworkspace/Anonymize/bin/init/:\
    /Users/(snip)/Eclipseworkspace/Anonymize/bin/anonymizer/:\
    /Users/(snip)/Eclipseworkspace/Anonymize/lib/:\
    /Users/(snip)/Eclipseworkspace/Anonymize/misc/"

    init mikes$ export PATH="${PATH}:${CLASSPATH}"

    init mikes$ pwd
    /Users/(snip)/Eclipseworkspace/Anonymize/bin/init

    init mikes$ java Init.class
    Error: Could not find or load main class Init.class

    init mikes$ java ./Init.class
    Error: Could not find or load main class ..Init.class

    init mikes$ java init.Init
    Error: Could not find or load main class java

**I'm not finding my mistake. What do I need to change/do, to get this code running from the command line?**


  [1]: https://stackoverflow.com/questions/18093928/what-does-could-not-find-or-load-main-class-mean
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 3 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 3 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros