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

Alex G.
  • 909
  • 1
  • 9
  • 16
Kubik Sukenik
  • 49
  • 1
  • 7
  • 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. – Luke Melaia Jun 04 '16 at 19:35

3 Answers3

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.

Community
  • 1
  • 1
Alex G.
  • 909
  • 1
  • 9
  • 16
  • It is work fine, but why I can't run in TEST folder? – Kubik Sukenik Jun 04 '16 at 19:57
  • 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. – Alex G. Jun 04 '16 at 19:59
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.

coder97
  • 17
  • 10
  • 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? – Kubik Sukenik Jun 04 '16 at 19:40
  • It has nothing to do with JDK. – Alex G. Jun 04 '16 at 19:46
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

Víctor
  • 416
  • 5
  • 15
  • 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. – Alex G. Jun 04 '16 at 20:02
  • Ok. Try this : CLASSPATH=$CLASSPATH:/test, after export $CLASSPATH, and finally java TEST – Víctor Jun 04 '16 at 20:08
  • Why would you do that? You can run java app from command line w/o changing CLASSPATH. – Alex G. Jun 04 '16 at 20:14