0

I am new to Java and I get this error for my code:

C:\Users\Unknown\Desktop\AIB>java GreenhouseControls -f examples1.txt
Restarting system
java.lang.ClassNotFoundException: ThermostatNight
        at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:268)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
        at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Class.java:164)
        at GreenhouseControls$Restart.action(GreenhouseControls.java:139)
        at tme3.Controller.run(Controller.java:32)
        at GreenhouseControls.main(GreenhouseControls.java:197)

I believe I have a classpath errors.

madhead
  • 31,729
  • 16
  • 153
  • 201

2 Answers2

0

Did you try to add your compiled java files (i.e., Event.class, ThermostatNight.class, ...) of Event.java, ThermostatNight.java to the classpath:

java -cp "target/*" GreenhouseControls -f examples1.txt

I assume that your compiled class files are in the target directory.

Philipp
  • 1,289
  • 1
  • 16
  • 37
  • C:\Users\Ali\Desktop\TME3>java -cp "C:/Users/Ali/Desktop/TME3/tme3/Events.class" GreenhouseControls -f examples1.txt Exception in thread "main" java.lang.NoClassDefFoundError: GreenhouseControls – A Mahesania Mar 21 '16 at 08:43
  • You have to add all .class files to your classpath. Typically your IDE compiles your classes for you, if not you have to compile them using `javac`. You should place all your .class files into a folder and you can add all files to the classpath with the mentioned syntax (see, e.g., http://stackoverflow.com/questions/219585/setting-multiple-jars-in-java-classpath) – Philipp Mar 21 '16 at 08:48
  • ThermostatNight is not in different package it exists in one file. Only Events.java is in a different file – A Mahesania Mar 21 '16 at 09:01
  • But you only add the Event.class in your classpath... you also have to add the .class file which contains the GreenhouseControls class and the ThermostatNight class – Philipp Mar 21 '16 at 09:04
  • This is what i get, once I did your trick java.lang.InstantiationException: GreenhouseControls$ThermostatNight at java.lang.Class.newInstance0(Class.java:335) at java.lang.Class.newInstance(Class.java:303) at GreenhouseControls$Restart.action(GreenhouseControls.java:140) at tme3.Controller.run(Controller.java:32) at GreenhouseControls.main(GreenhouseControls.java:197) – A Mahesania Mar 21 '16 at 09:07
  • Ok thats a different exception can you open a new post and show the full stack trace? – Philipp Mar 21 '16 at 09:15
0

I solved in this way.

java -classpath YourProjectWar.jar com.yourpackage.ClassWithMain -f examples1.txt
Andrea Girardi
  • 4,337
  • 13
  • 69
  • 98