1

I would like to compile and execute a program in Java with Command Prompt but only compile the program: javac MyApp.java

Then I want to execute with that command: java MyApp but it throws an error:

Error: Could not find or load main class MyApp

I think that is possible to be a problem with JRE or JDK configuration but I check the path, java -version and 'Build path' in eclipse project but nothing. Any idea?

Neil
  • 322
  • 1
  • 2
  • 13
  • Check your [classpath](http://docs.oracle.com/javase/7/docs/technotes/tools/windows/classpath.html). That determines where Java looks for the compiled `.class` files. – khelwood Nov 06 '14 at 16:09
  • Your class MyApp has a public static void main(String[] args){} function inside? – punseti Nov 06 '14 at 16:15
  • @punseti yes with simple hello world for testing :) – Neil Nov 06 '14 at 16:18
  • Ok @khelwood, but I don't find the command. The link is for set a classpath – Neil Nov 06 '14 at 16:23
  • See http://stackoverflow.com/questions/18093928/what-does-could-not-find-or-load-main-class-mean – khelwood Nov 06 '14 at 16:26

1 Answers1

2

Maybe you have to add to your classpath the directory of MyApp.class. Try this at the command prompt

          java -cp . MyApp

EDIT

You have to create a folder named 'ok' and you have to put MyApp.class inside that folder. Then in the parent folder of the ok folder execute

          java -cp . ok.MyApp

Maybe even java ok.MyApp should work

amedeo avogadro
  • 595
  • 2
  • 7
  • 14
  • I tried but throws the same error: `Error: Could not find or load main class MyApp` :/ – Neil Nov 06 '14 at 16:36
  • @NeilPlain Do you have a package declaration inside MyApp.java? – amedeo avogadro Nov 06 '14 at 16:39
  • 1
    @NeilPlain but did you execute the command from the parent directory of 'ok'? (as i wrote on my last edit). On my filesystem I have 'C:\prova\ok\MyApp.class' and I executed the command from 'C:\prova\ – amedeo avogadro Nov 06 '14 at 16:54
  • Solved. I was writing the command into package folder. And I tried into 'src' folder and solved! Thanks at all. – Neil Nov 06 '14 at 16:54