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
Related
So I wrote same question yesterday but it was closed due to the lack of specifics.
I'm simply trying to run my Main class by terminal. My code:
public class Main {
public static void main(String[] args) {
System.out.println("Hello, Java");
}
}
My classpath: C:\Users\48790\IdeaProjects\workingWithCommand\src
In terminal : Directory of C:\Users\48790\IdeaProjects\workingWithCommand
I've tried : -cp C:\Users\48790\IdeaProjects\workingWithCommand\src and
set CLASSPATH = C:\Users\48790\IdeaProjects\workingWithCommand;C:\Users\48790\IdeaProjects\workingWithCommand\src
Still: file not found: Main.java when typing : javac Main.java
Thanks for helping :)
The compiler (javac) does not use the classpath for the files given as argument.
try javac src\Main.java
or
change to src (cd src) and then javac Main.java
The file given as argument to javac is a file as used for any other command of that operation system. The classes used by the program are searched using the classpath or -cp.
(to execute a class you use java Main (not java Main.class) because the argument is not a file1, but a class name - this is searched using the classpath or -cp option)
1 - with newer Java versions, a single source file can be executed and compiled on the fly by : java Main.java (now it is a file, similar to a script)
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!
I have a .java file which i compiled in a package named "Mypack",using command line as follows
javac -d . file_name.java // The "." specifies the current working directory which was the desktop
so it creates a folder on the desktop named "Mypack"(The package name), in the folder the .class file for my program is placed.Now i did the following
java -classpath "C:\Users\LoRd CuRZon\Desktop\Mypack" file_name // Error Could not find or load main method
Even if i go into the directory "Mypack" and launch command prompt from that directory and try to run the program i still get the same error.
run it as likewise from Desktop,
c:/.../Desktop> java Mypack.file_name
java command requires fully qualified name .
so from desktop run java Mypack.classname
If you have this error:
Error Could not find or load main method
That means you don't write a main method in you code try to write it.
But befor to do those steps:
Fo compiling a programme do this:
java Mypack.file_name
To run it do like this:
java Mypack.classname
I have problem running a basic helloworld application in Java form my command-line in widnows 7. I can run it in Java.
Here is my code(in NetBeans):
package helloworld;
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
I have set C:\Program Files\Java\jdk1.8.0_20\bin; on my PATH variable in the Windows Environment.
When running :
javac HelloWorld.java
the HelloWorld.class is built successfully.
However in the next step when I run:
java HelloWorld
I get he following error:
Error: Could not find or load main class HelloWorld
Under my program source root directory I can see these two file:
. HelloWorld.class
. HelloWorld.java
What am I missing please?
You should specify fully qualified class name. That is, you need to run it like that: java helloworld.HelloWorld.
what you need to do is as you have
package helloworld;
and you are trying to execute it from the commandline
do the following steps
First open the terminal or cmd and browse to the folder helloworld.
Example if your helloworld folder in in f:/helloworld open the terminal and browse upto f:/(don't go inside helloworld)
then compile the class as javac helloworld/HelloWorld.java
and try executing the class as java helloworld.HelloWorld
the class name was not fully qualified, try java helloworld.HelloWorld
the .classfile should not be in the directory the java command is run from.
you forgot the helloworld package. So you have to enter java helloworld.HelloWorld to make it work. Next time please use a different package name than the java file, so there is no confusion. :)
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.