1
package test 
    public class test{
        public static void main (String args[]) throws Exception{

            System.out.println("testing");
       }
     }

I compile with the following command

javac test.java  -Xlint -g

I execute with the following command

java test 

Which gives me the error

Error: Could not find or load main class test

Now I strongly suspect something weird is going on with my java as eclipse is also crashing whatever I do.

10
  • 1
    Did it produce the test.class file? Commented Feb 18, 2016 at 21:03
  • 1
    Long time ago since I wrote me some java, but don't you need to start your classes with a big letter (Test)? Commented Feb 18, 2016 at 21:03
  • @Binke That's just convention. Classes can start with lower case letters. Commented Feb 18, 2016 at 21:05
  • @Binke No, you don't have to. But it is a very common convention to do that in order to distinguish classes from variables. Commented Feb 18, 2016 at 21:06
  • @AndrewWilliamson This does produce a test.class Commented Feb 18, 2016 at 21:06

4 Answers 4

2

Your classpath doesn't have current directory which is . (dot). Add it or put your test.class to the existing path and it will be OK.

Easiest way is to run java like this:

java -classpath . test

Read more about classpath here.

Sign up to request clarification or add additional context in comments.

2 Comments

How do you mean? java .Test has the same problem as java Test
The current directory should always be within the classpath (as long as you do not set it explicitly). @Thijser try running java -cp . test
1

I have copied your code and executed it using the same commands as you have specified, and it displays 'testing' as the output.

By any chance, Is the class 'test' in a package? If so, you can compile it successfully by entering into the package directory and then running the compile command but it gives an error while you run it from that directory. You gotta come one level up the directory and run the class.

Eg: if the test.java is in package called abc, then you could go to 'c:\example\abc\' where c:\example is an directory assumed, and compile it and it would compile just fine. But for running it, you gotta go one level up the directory and run. So you got to go to c:\example\ and then run like this 'java abc.test'

5 Comments

If the class were in a package, it would not compile, but OP can compile the file.
This was correct it was indeed in a package but I did not think that would matter.
@Turing85 - I think it would compile just fine as long as you are in the package directory
I was in the directory
Yeah so that is why it was compiling. But in order to run it, you need to specify the package.
1

Are you defining your class under a package, if so please use fully qualified path i.e package/className while executing ?

Comments

1

If the class is part of a separate package, then you just need to add that package to the classpath as mentioned above. Also, as long as you did not specify a file path for the class file to be stored in the commands you have given should function properly. The main idea is to have the java file compile into a class file in the same directory, and upon executing the java command on that file it searches for the class file with the name you have given to the console to execute.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.