2

I'm trying to run a program written in Java using the command prompt on my Mac , but after compiling me pop up message: Error: Could not find or load main class TEST.

Maybe Im doing stupid mistake.

This is code of java program (I'm using NetBeans):

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package test;

/**
 *
 * @author Kuba
 */
public class TEST {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        System.out.println(“Hello”);
    }

}

This is from my command line:

>     Last login: Sat Jun  4 18:35:23 on ttys000
>     Jakub-MacBook-Pro:~ Kuba$ ls
>     Applications      Library         Pictures
>     Desktop           Movies          Public
>     Documents     Music           VirtualBox VMs
>     Downloads     NetBeansProjects
>     Jakub-MacBook-Pro:~ Kuba$ cd NetBeansProjects/
>     Jakub-MacBook-Pro:NetBeansProjects Kuba$ ls
>     JavaApplication1  TEST
>     Mocnina           VypocetObvoduaObsahu
>     Jakub-MacBook-Pro:NetBeansProjects Kuba$ cd TEST
>     Jakub-MacBook-Pro:TEST Kuba$ ls
>     build     build.xml   manifest.mf nbproject   src
>     Jakub-MacBook-Pro:TEST Kuba$ cd src
>     Jakub-MacBook-Pro:src Kuba$ ls
>     test
>     Jakub-MacBook-Pro:src Kuba$ cd test
>     Jakub-MacBook-Pro:test Kuba$ ls
>     TEST.class    TEST.java
>     Jakub-MacBook-Pro:test Kuba$ javac TEST.java
>     Jakub-MacBook-Pro:test Kuba$ java TEST
>     Error: Could not find or load main class TEST
>     Jakub-MacBook-Pro:test Kuba$

Thank you for any advice. JS

1
  • If you're using NetBeans, why are you trying to compile the class with the command line? NetBeans has both a compile (build) and run option at the top of the window. Commented Jun 4, 2016 at 19:35

3 Answers 3

1

Jakub-MacBook-Pro:test Kuba$

If you have defined a package in your file than you have to run java command outside the package folder and call the class with package name.

This should do the trick.

cd ../
java test.TEST

Search for classpath. This is also a good read.

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

2 Comments

It is work fine, but why I can't run in TEST folder?
You can, but you need to set classpath anyway. For example, this should work in test folder java -cp ".." test.TEST. Please, read some manuals about it. This is basics of Java. Hope it helps.
0

Try reinstalling your JDK. Sometimes when this happens your JDK cannot be located. Make sure it is in the same folder as your IDE locates it in.

2 Comments

I reinstalling JDK but same error. I don't know what you mean with - "make sure it is in the same folder as your IDE locates it in" - you mean that TEST.java file must be in IDE folder?
It has nothing to do with JDK.
0

Try this : java -cp . TEST on the folder where the .class is located. -cp means classpath. You can also add to the CLASSPATH environment variable the folder where TEST is located and run : java TEST as you tried. Basically the problem is that java is not able to find TEST class

3 Comments

TEST.class is located in the test/ folder. If you run the java -cp . TEST command in that folder, the same error Error: Could not find or load main class TEST will be displayed. Your answer is incorrect.
Ok. Try this : CLASSPATH=$CLASSPATH:/test, after export $CLASSPATH, and finally java TEST
Why would you do that? You can run java app from command line w/o changing CLASSPATH.

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.