Error: Could not find or load main class xxx Linux - java

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?

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

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

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.

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.

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.

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.

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.

Related

Error: Could not find or load main class ( in Java 8)

I am trying to learn Java and I've made my first program and compiled it into a class file (the file is called aye.java and when compiled I have aye.class, I think the compilation worked). However when I use the java command in the folder where the class is located it just returns below error -
Could not find or load main class aye.class.
I have tried including the package name (com.java24hours) but it still doesn't work.. please help!
Commands I have tried:
java aye.class
java com.java24hours.aye.class
java aye
java com.java24hours.aye
program code:
package com.java24hours;
class aye {
public static void main(String[] args) {
//java code yeet
String aye = "Hello World!";
System.out.println(aye);
}
}
(I am running Linux on a Chromebook and have installed Java via the ppa:webupd8team/java)
Thanks.
I suppose you wanna put binaries to ./bin folder.
Compile aye.java:
javac -d ./bin aye.java
Then cd to ./bin directory and run the program:
cd bin
java com.java24hours.aye
well im stupid
since im new to java, i didn't know anything about packages and such. turns out all i had to do was put the class file in a folder named "ya" (that's the name of the package - i updated the program) and run the command
java -cp /home/ramsey/Documents/ya aye
(-cp stands for classpath, and you use it when you want to specify where you class is located MAKE SURE TO PUT IT IN A FOLDER NAMED AFTER YOUR PACKAGE!!!)
the wiki page is helpful: https://en.wikipedia.org/wiki/Classpath_(Java)
its under the section setting the path to execute java programs
thanks for the help everyone!

Eclipse compiled files are not runnable by java command

When you write a simple Java Application in Eclipse in automatically compiles those files and stores them in the bin/ folder of the root folder of the project.
Now if I navigate to the /bin folder and to the folder that contains the .class file I want to run via the java command below I am getting the following error - :
java A
Error: Could not find or load main class A
Class A:
package assurance;
public class A {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
While the class A has a main method and runs fine when I right click on the file and do a Run As Java Application. But it does not run from the command like java command.
Why is this happening ?
Update:
Tried with the following commands-:
java -cp "A.class" assurance.A
java -cp "A" assurance.A
java -cp "*" assurance.A
It works in Eclipse, because Eclipse just runs it with correct -cp and correct command :)
Run your code with the following command:
java -cp "./" assurance.A ("" for some odd cmd interpreters like Windows XP)
it is important that the command is run from the "default package" directory (top-level package directory).
Java interprets package name (assurance) as directory path to the class file. Imagine if it replaces . with / and adds .class extension
(assurance.A => ./assurance/A.class)
More details here: http://docs.oracle.com/javase/7/docs/technotes/tools/windows/classpath.html

Running a java program through command line

I am running a Java program with the following command:
java -cp .:./* com.bot.fix.botclient
All the jar files are in the same directory. It works FINE! But what if I want to run it from a different folder?
The full location of the java program is: FIX/fixprog/src/com/fix/botclient
But if I try to run:
java -cp FIX/fixprog/src/* FIX/fixprog/src/com.bot.fix.botclient
I get:
Error: Could not find or load main class FIX.fixprog.src.com.bot.fix.botclient
What am I doing wrong? How can I run the same Java program but not in the same directory?
If you have only jar files try:
java -cp FIX/fixprog/src/* com.bot.fix.botclient
If you have also classes you should try:
java -cp FIX/fixprog/src/*:FIX/fixprog/src/ com.bot.fix.botclient
If both did not work perhaps you shoud use absolut path with disk unit if you are using windows.
Try if below works. You don't need to specify path when giving the fully qualified name of your java class that you are trying to execute. The "cp" part takes care of it.
java -cp FIX/fixprog/src com.bot.fix.botclient

Running Java Programs on the Command Prompt

I have done a lot of researching on this concept but I can't seem to run a java program on the command prompt. Let's say we had a simple program like this:
public class Hello_World {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
On the command prompt I tried:
javac Hello_World.java
But I get:
'javac' is not recognized as an internal or external command, operable program or batch file
So I compiled it on BlueJ and then did this:
java Hello_World.java
But it said "cannot load or find main class Hello_World"!
I am currently using Windows 7, and made the programs on Notepad++ and BlueJ (to compile).
Any suggestions? Thanks!
This explains in detail what you have to do to set class path. Primarily you need to set your environment variables so that your shell finds the right directory containing javac to compile your program
javac' is not recognized ..
comes when you haven't point your java bin directory to your path environment variable. Because bin directory is the place where javac.exe exist.
To do it.
1) right click on mycomputer property
2) go to Advance system settings.
3) go to environment variable.
4) In system variable click on path
5) go to edit mode and provide your path to java bin directory.
in my case it is C:\Program Files\Java\jdk1.7.0_01\bin;
'javac' is not recognized as an internal...
means OS does not know where javac program is located. Either add it to PATH or run explicitly
my\path\to\file\javac Hello_World.java
Compiling will convert *.java to *.class
Hello_World.class file should be located according to it's package directive. Since you have no one, in your case it should be located in the same directory you will run java.
To run your class specify it's name not file name
java Hello_world
looking for the class is essential part of launching and occurs by rules.

I have managed to compile java-program but I cannot execute it

I have just installed JDK on Windows Vista. After that I set proper values for the 4 environment variables: classpath, include, lib, path. After that I was able to compile my HelloWorld-program (I got a *.class file). But when I try to execute the compiled program (I type java HelloWorldApp) it does not work. The Java write a lot of stuff and in the end it is written that it "could not find the main class: HelloWorldApp". Can anybody, pleas, help me with this problem?
Just for clarity; you are saying that you have a class in the default package, that is you have not included a package specifier in the Java file, and your class is called HelloWorldApp. When you compiled this, you got a classfile HelloWorldApp.class in the current directory.
Assuming the above to be true then try:
java -cp . HelloWorldApp
For example, the following works on a unix box:
$ echo 'class HelloWorldApp { public static void main(String []argv) { System.out.println("Hello World!"); } }' > HelloWorldApp.java
$ javac HelloWorldApp.java
$ java -cp . HelloWorldApp
Hello World!
Of course, you should indent your code a little nicer than just shoving the whole thing onto one line ;-)
Edit: To answer the comment:
Normally, the default classpath is the runtime libraries and the current directory. However, if you have the CLASSPATH variable set, then this will override the default, and you need to explicitly set the classpath back to its "default value". To verify if the CLASSPATH environment variable is set, you can do (again assuming unix):
set | grep CLASSPATH
If it is set, that is why you need to manually include . on your classpath.
create a file called HelloWorld.java;
paste the code posted below inside HelloWorld.java:
compile it by executing the command: javac HelloWorld.java in the same folder as HelloWorld.java is in;
execute the code by doing: java -cp . HelloWorld in the same folder as HelloWorld.java is in.
public class HelloWorld {
public static void main(String[] args) {
System.out.println("HelloWorld works!");
}
}
How the classpath works, can be read here: http://en.wikipedia.org/wiki/Classpath_%28Java%29
Have you included . and .. in your path? Just for clarification . represents your current directory and .. represents your parent directory. You are telling that the java has to search the current directory and the parent directory to find the class. Add the same to your classpath too.
What happens if you use:
java -cp {path to directory with HelloWorldApp in it} HelloWorldApp
That path should be contained within your CLASSPATH environment variable. Is that exported to your command shell ? Do you need to start a new command shell to get the most recent version of CLASSPATH ?
Post your code. I believe the problem is that your main class is not defined properly. I did this the other day.
public static void main(String[] args){
//code
}
The class path concept and the logical difference between Java source code and compiled byte code is notoriously hard to get right.
I would strongly recommend you familiarize yourself with the Sun Java Tutorial. The relevant section is
http://java.sun.com/docs/books/tutorial/getStarted/cupojava/win32.html

Categories