user1660325
user1660325

Reputation: 757

Error: Could not find or load main class xxx Linux

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?

Upvotes: 13

Views: 65670

Answers (7)

user1651008
user1651008

Reputation: 27

if you want to run program in current working directory where your class reside.

java gives three options.

first option

java -cp Tester

Second option for current working directory

java -cp . Tester

Third option export CLASSPATH variable

export CLASSPATH=$CLASSPATH:. (this is the best one if your directory changes) or

export CLASSPATH=$PWD

or

export CLASSPATH=

after that you must sorce the bashrc or bashprofile.

Upvotes: -1

Chaitanya
Chaitanya

Reputation: 132

Before Specifying the path,ensure you follow these three things meticulously, 1. Close the command prompt window, before specifying the path. 2. When adding path, add bin and semi- colon at the end and 3. If JAVAC command has worked properly, try java -cp class name.

Upvotes: 0

Ascendant
Ascendant

Reputation: 2579

I had the exact same issue on windows, and I solved it by adding path "." to both CLASSPATH and PATH, maybe you can try this on Linux as well.

Upvotes: 3

James Liu
James Liu

Reputation: 21

We first know javac command work well.

I also met this error,and i have resolved this.Let me share this.

First we need to find the parent path of your package in your java codes.

Then cd to that path using java package + fileName should work well at that moment.

Upvotes: 2

Batakj
Batakj

Reputation: 12743

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   

Upvotes: 16

mthmulders
mthmulders

Reputation: 9705

Your .class file should not reside in /home/scripts/, but in /home/scripts/com/util/. Take a look at this document that explains the relation between classpath, packages and directories.

Upvotes: 1

L. Quastana
L. Quastana

Reputation: 1336

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

Upvotes: 2

Related Questions