Error: could not find or load the main xxx Linux class

I am very new to linux environment.

I am trying to run a simple hello world Java class in linux environment.

Hey. java

package com.util; public class Hello { /** * @param args */ public static void main(String[] args) { System.out.println("hi"); } } 

I compiled the java class on Windows and uploaded the .class file to the linux system in the path / home / scripts.

my command is as follows:

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

when I execute this command from the same / home / scripts where Hello.class I get

Error: could not find or load the main class com.util.Hello and continue no further.

Can someone help me on this?

+7
java linux shell
source share
7 answers

go 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 
+6
source share

First you must generate your .class file:

javac./hello.java

This command created the hello.class file And after you can run your class file! :)

java hello

+2
source share

down vote

if you want to run the program in the current working directory where your class is located.

java provides three options.

first option

Tester java -cp

The second option for the current working directory

java -cp. Tester

The third option exports the CLASSPATH variable

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

export CLASSPATH = $ PWD

or

export CLASSPATH =

after that you should evaluate bashrc or bashprofile.

+1
source share

At first we know that the javac team works well.

I also met this error and I solved it. Let me share this.

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

Then cd to this path using java package + fileName should work well at this point.

+1
source share

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

+1
source share

Your .class file should not be in /home/scripts/ , but in /home/scripts/com/util/ . Take a look at this document , which explains the relationship between classes, packages, and directories.

0
source share

Before specifying a path, make sure that you carefully monitor these three things: 1. Close the command prompt window before specifying the path. 2. When adding a path, add a hopper and a half-point to the end and 3. If the JAVAC command works correctly, try the class name java -cp.

0
source share

All Articles