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

Mike R.
Mike R. used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2016
Commented:
Try
java -cp /Users/(snip)/Eclipseworkspace/Anonymize/bin init.Init

Open in new window


When they're done in the IDE, Java apps should be in executable jars (hint: 'export' in Eclipse)

Author

Commented:
OK. That was it :) I tried a half doze variations of that I found on the web, but I was missing the `package` prefix. Thank you. I'll look for a link on how to export.
Top Expert 2016

Commented:
:)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial