Java HelloWorld commandline - java

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. :)

Related

Run Java with third party JAR

I've been trying for 1 week to run a simple java program using third party jar, but I keep getting error. Just now I try this program as below
import org.apache.commons.lang3.*;
public class HelloWorld{
public static void main(String[] args){
String x = "abcd";
System.out.println(StringUtils.capitalize(x));
}
}
and when I try to compile and run, command prompt return error as below
c:\training\java\exercise>javac -cp ".;./org/apache/commons/lang3/commons-lang3-3.9.jar" HelloWorld.java
HelloWorld.java:5: error: cannot find symbol
System.out.println(StringUtils.capitalize(x));
I take this example from this sites as for reference https://www.programcreek.com/2014/01/compile-and-run-java-in-command-line-with-external-jars/
can anyone help me how to solve this? Thanks in advance
enter image description here
There is an article about just that available here
I assume you're on Windows as the working folder has disk C: so it should be Windows.
Bottom line it looks like you're mixing the notions of folder and jar.
Make sure that the folder C:\training\java\exercise contains both HelloWorld.java and commons-lang3-3.9.jar
Also make sure you're in the C:\training\java\exercise folder
Then compile like this:
javac -cp ".;./commons-lang3-3.9.jar" HelloWorld.java
Now this should create HelloWorld.class file
Now Run the java process:
java -cp ".;./commons-lang3-3.9.jar" HelloWorld

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!

java able to compile but unable to find class error with cmd

Hello so recently I have started to transfer from c++ to java and one exercise is to compile and run a java program using cmd.
So okay, I coded my simple HelloWorld program using netbeans and saved it,
package helloworld;
public class Helloworld
{
public static void main(String[] args)
{
System.out.println("Hello world");
}
}
so now my saved .java file is in C:\Users\eatmybuns\Documents\NetBeansProjects\Helloworld\src\helloworld
now I open the cmd and I change the directory to the above and typed
javac Helloworld.java
and now I can see Helloworld.class in the same folder, I read from somewhere that I have to include the package name as well for it to run so I typed
java helloworld.Helloworld
it gave me an error so I tried running it from the src folder instead but it also gave me the same error.
Error: Could not find or load main class Helloworld
Caused by: java.lang.ClassNotFoundException: Helloworld
I have read some possible solutions such as using -cp or using -d but it keeps giving me the same error. I am currently using jdk1.8.0_161. on windows 10.
You have to use
java helloworld.Helloworld
and from the parentfolder of helloworld, which is the src directory, in your case.
There is a tight relationship between package and directory structure.
There are many flags you can set for the compiler, like srcdir, targetdir to keep classes and sources apart. But basically, when you invoke your class helloworld.Helloworld, the JVM looks for a directory helloworld/ and expects a Helloworld.class there.
To achive this without compiler flags, you have to put the source into the helloworld/ folder too.
The whole name of your class is helloworld.Helloworld and java should look there and find it there.
mkdir helloworld
mv Helloworld.java helloworld/
javac helloworld/Helloworld.java
java helloworld.Helloworld
Hello world
It's a bit surprising in the beginning, if you don't know it and started with classes without package declaration. But the logic is simple and straight forward: Every package is reflected by the directory structure.
With a distinction of sourcedir and targetdir, the directory structure below has to be the same as without, just the starting point differs. Common target dirs are classes or bin, like in:
javac -s . helloworld/Helloworld.java -d classes
or
javac -s ./src helloworld/Helloworld.java -d bin
But bin or classes don't get part of the package name, and you can't extend the invocation of the class by prepending that dir to the invocation path:
java bin.helloworld.Helloworld
won't work. But
java -cp ./bin helloworld.Helloworld
should. If you read the documentation carefully, you will find, that it carefully distinguishes source file (Helloworld.java), class (Helloworld) and file (Helloworld.class).

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

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?
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.

Categories