Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: Could not find or load main class xxx Linux

Tags:

java

linux

shell

I am very new to linux environment.

I am trying to run an simple hello world java class in linux environment.

Hello .java

 package com.util;

    public class Hello {

        /**
         * @param args
         */
        public static void main(String[] args) {
            System.out.println("hi");

        }

    }

I have compiled java class in windows environment and uploaded the .class file to linux system into /home/scripts path.

my command is as follows,

java -cp /home/scripts com.util.Hello

when i am executing this command from this same /home/scripts where Hello.class is there i am getting,

Error: Could not find or load main class com.util.Hello and not able to proceed further.

can some one help me in this issue?

like image 706
user1660325 Avatar asked Jul 19 '13 10:07

user1660325


People also ask

Could not find or load main class in java in Linux?

The error 'Could not find or load main class' occurs when using a java command in the command prompt to launch a Java program by specifying the class name in the terminal. The reason why this happens is mostly due to the user's programming mistake while declaring the class.

What is java cp command?

Java -cp is a parameter in the Java Virtual Machine or Java compiler. The -cp, or CLASSPATH, is used as an option to the Java command. It is a parameter in the Java Virtual Machine or Java compiler that specifies the location of classes and packages which are defined by the user.


2 Answers

navigate to /home/scripts using terminal

javac com/util/Hello.java 

then

cd /home/scripts
java -cp . com.util.Hello

Or,

java -cp "/home/scripts" com.util.Hello   
like image 69
Shashi Avatar answered Oct 20 '22 05:10

Shashi


At first you must generate your .class file :

javac ./hello.java

This command has generated hello.class file And after you can run your class file ! :)

java hello

like image 2
L. Quastana Avatar answered Oct 20 '22 04:10

L. Quastana