0

I have this below proram

package com;

    import java.io.PrintStream;
    import java.net.URL;
    import java.net.URLConnection;

    public class Caller
    {
      public static void main(String[] args)
      {
        try
        {
          URL url = new URL("http://localhost:8080/Test/Ravi.jsp");
          connection = url.openConnection();
        }
        catch (Exception ioe)
        {
          URLConnection connection;
          System.err.println("IOException: " + ioe.getMessage());
        }
      }
    }

When i am running this i am getting

C:\Program Files\Apache Software Foundation\apache-tomcat-6.0.33\webapps\Test\WEB-INF\classes>java com.Caller

Exception in thread "main" java.lang.NoClassDefFoundError: com/Caller
Caused by: java.lang.ClassNotFoundException: com.Caller
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
Could not find the main class: com.Caller.  Program will exit.
Pavan Kumar
  • 211
  • 1
  • 5
  • 8
  • probably a silly doubt, but I hope you have made sure that Caller.class exists under C:\Program Files\Apache Software Foundation\apache-tomcat-6.0.33\webapps\Test\WEB-INF\classes\com ? in case it doesn't, you would have to compile to source using javac. As Jon says in his answer, it is unclear as to why you are trying to run the class from inside tomcat directory – aishwarya Nov 25 '11 at 06:29
  • possible duplicate of [Causes of 'java.lang.NoSuchMethodError: main Exception in thread "main"'](http://stackoverflow.com/questions/5407250/causes-of-java-lang-nosuchmethoderror-main-exception-in-thread-main) – Stephen C Nov 25 '11 at 06:34
  • @StephenC: No, it's a different exception, with a different cause. – Jon Skeet Nov 25 '11 at 08:57

3 Answers3

5

It's not at all clear why you're trying to run a console app from a Tomcat directory, but basically it can't find your class.

You should probably have a com directory under your current directory, and a Caller.class file in that directory. If that's not the case, you need to work out how you've compiled the code, and fix it.

My guess is that you've just run:

javac Caller.java
java com.Caller

That will have created the class file in the current directory. There are two ways of fixing this. You could just use -d:

javac -d . Caller.java
java com.Caller

Or you could move the source file into a structure which reflects the package structure, i.e. create a com directory yourself:

mkdir com
move Caller.java com
javac com\Caller.java
java com.Caller

Or even better, you could separate out source from binary files:

mkdir src\com
move Caller.java src\com
javac -d bin src\com\Caller.java
java -cp bin com.Caller
Jon Skeet
  • 1,261,211
  • 792
  • 8,724
  • 8,929
-1

You must ensure that you add the location of your .class file to your classpath, when running the java command (so that the execution can find your compiled class).

So, go to the com directory, and use:

java -cp . Caller
Saket
  • 42,173
  • 11
  • 56
  • 76
  • 1
    That will fail saying "wrong name: com/Caller" or something similar. The Caller class is declared to be in the com package. – Jon Skeet Nov 25 '11 at 06:25
  • guess i was just a bit late in updating my answer :) – Saket Nov 25 '11 at 06:26
  • 1
    I'm not sure what you mean - it's still incorrect now. The OP *shouldn't* do this; it won't work. – Jon Skeet Nov 25 '11 at 06:29
  • The classes exist , i cam not able to post the picture ,bu still the error persists , did exactly the same . – Pavan Kumar Nov 25 '11 at 06:43
  • @PavanKumar: They may exist, but quite possibly in the wrong place. You need to give us more information (ideally including why you're doing all of this in a *web-app* directory in the first place). – Jon Skeet Nov 25 '11 at 07:59
-1

It means that the class in not exist. Pls. try to build your project again and deploy it in web server.

Jwalin Shah
  • 2,043
  • 1
  • 15
  • 19
  • 1
    Well, it may exist but in the wrong place. Personally I wouldn't recommend deploying command line apps in web servers in the first place... – Jon Skeet Nov 25 '11 at 06:29
  • yup.. you are right. he needs to put it as a right place and proper package structure – Jwalin Shah Nov 25 '11 at 06:35