java compilation: classname Vs classname with file-extension - java

Running basic java programs from commands line is a 3 steps process:
Write code:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World");
}
}
Compile by javac HellWorld.java which would check for errors & generate HelloWorld.class file.
run code by giving the class name --> java HelloWorld
Now,
I am curious to know why:
java HelloWorld works but when we give fullpath of the classfile, it throws an error
$ java HelloWorld.class
Error: Could not find or load main class HelloWorld.class
What does it make a difference if we give just the classname Vs classname with file-extension?

What does it make a difference if we give just the classname Vs classname with file-extension?
The argument you give to the java binary isn't meant to be a filename. It's meant to be a class name. So in particular, if you're trying to start a class called Baz in package foo.bar you would run:
java foo.bar.Baz
So similarly, if you try to run java HelloWorld.class it's as if you're trying to run a class called class in a package HelloWorld, which is incorrect.
Basically, you shouldn't view the argument as a filename - you should view it as a fully-qualified class name. Heck there may not even be a simple Baz.class file on the file system - it may be hidden away within a jar file.

What does it make a difference if we give just the classname Vs classname with file-extension?
It is because that is the way it is. Sun / Oracle have implemented the java command to work that way since Java 1.0, and changing it would be massively disruptive.
As Jon says, the argument to the command is a fully qualified class name, not a filename. In fact, it is quite possible that a file with the name HelloWorld.class does not exist. It could be a member of a JAR file ... or in some circumstances, just about anything.
In Java 11 and later it is also possible to compile and run a single Java source file with a single command as follows:
java HelloWorld.java
(This possible because Oracle no longer supports Java distributions without a Java bytecode compiler.)

In the Java programming language, source files (.java files) are compiled into (virtual) machine-readable class files which have a .class extension.
When you run java class file after compile then run the following command:
java HelloWorld
Note: Need to setup java classpath

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!

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

Is it possible to run Java program from command line without compiling?

I have a set of instructions to create a Java application that takes multiple arguments when running the application from a CMD line.
The instructions state:
Thus, using the example set of above, assuming the main() method of your program is in a class called JavaClassName, the output should be:
$ java JavaClassName 4 7 file.csv
program output here
My question is:
Isn't this skipping the compile process?
Would they assume that loading the Java classes onto a computer that has never run this application before (or a directory with purely the .java files needed to run); running the cmd
$ java JavaClassName 4 7 file.csv
would output something?
Sidenote: Currently, running that CMD outputs
Error: Could not find or load main class JavaClassName
Have ran through multiple SO questions and online tutorials trying to get this to even run but I have yet to get it to work.
You ask:
Isn't this skipping the compile process?
Absolutely yes. A command line like java JavaClassName 4 7 file.csv assumes that there is a compiled class file "JavaClassName.class" in the current directory (or in some other directory or Zip/Jar file found in the CLASSPATH environment variable). And yes, to produce that "JavaClassName.class" class file, you have to use a java compiler first.
from Java 10 it is possible to run java programs that fit a single file without manually run the compiler first.
It will be compiled on the fly before execution. Nice and useful for scripting.
e.g. writing HelloWorld.java file
public class HelloWorld{
public static void main(String[] args) {
System.out.println("Hello World");
}
}
we can run it as
java HelloWorld.java
We can also add more classes in the single file.
from Java 11 we can also use shebang files
we have to specify which version of the java language you want to use
we have to save the file without .java extension
remember to give executable permissions chmod +x HelloWorld
writing HelloWorld file
#!/path/to/java --source 11
public class HelloWorld{
public static void main(String[] args) {
System.out.println("Hello World");
}
}
and we can run it as
./HelloWorld

Javah tool error: could not find class file for HelloWorld

I am trying to create a header file using javah tool from the command line and an external tool configuration on eclipse on windows 7 OS but it's not working.
My code is:
package mypackage;
public class HelloWorld {
private static String HelloWorld;
private native void print();
static {
System.loadLibrary(HelloWorld);
}
public static void main(String[] args)
{
new HelloWorld().print();
}
}
I have followed different ways and even read the documentation of javah tool from Oracle, but they didn't help to overcome this problem.
My class file (HelloWorld.class) and java file (HelloWorld.java) both are in C:\..\eclipse-workspace\Distribution System Process\src\mypackage
But whenever I run javah tool, it gives me an error:
could not find class file for HelloWorld or mypackage.HelloWorld
I tried by providing classpath as well, but am not getting any header file.
Note: I have two classes in my package. The Frame1.java is the main class, which is the Gui, and the other class is used for JNI and called HelloWorld.java. I am not sure if the classes matter, but I am currently working on HelloWorld.java to create a header file:
What is causing this failure? :(
javah tool requres access to compiled code. You have to provide place, where your compiled class is.
Take a look here for a very simple sample code:
http://jnicookbook.owsiak.org/recipe-No-001/
As you can see, sources are compiled and stored inside some other place (here it is called target)
${JAVA_HOME}/bin/javac -d target java/recipeNo001/HelloWorld.java
${JAVA_HOME}/bin/javah -jni -d c -cp target recipeNo001.HelloWorld
Then, you have to tell javah where to find these files (compiled Java classes). It is done by -cp argument.
Argument -d, on the other hand, will tell javah where to store C headers.

Basic Java - Packaging

I decided to post this question after trying to find an answer for it, and couldn't find one.
I'm studying for OCJP and tried few simple codes. This is what I did and need to do.
Created two .java sources, say TestOne.java, TestTwo.java [using
notepad++]
Created a directory named "package1" and placed the two sources in
them.
Both the source files have "package package1;" as their first statement.
TestOne.java has one public class and one class with default access.
TestTwo.java has one default class with an object of the default
class in TestOne.java.
The main method is in this default class in TestTwo.java. It tries to invoke a method in
the referred object that was created, using TestOne.java default class.
So after all that was set up compiled TestOne.java then TestTwo.java by setting the flag "classpath" in javac [ javac -classpath ]. Complied. But when I tried to run it gave me an exception "Exception in thread "main" java.lang.NoClassDefFoundError". Does anyone know what's wrong ?
run the code after compile
compile javac TestTwo.java
run after compile java TestTwo
try this
javac -d path cname.java
so write the code like this
javac -d c:\main testone.java javac -d c:\main testtwo.java
c:\main should exist in your pc
then while executing
java -cp path pn.classname
so whichever class contains main (say test2)
java -cp c:\main package1.testtwo

Categories