Compiling java class from command line defined in a package [duplicate] - java

This question already has answers here:
What does "Could not find or load main class" mean?
(61 answers)
Closed 1 year ago.
I'm able to compile and run a simple hello world program from command line with
javac hello.java
java hello
however, if I had a package statement at the top package com.mypackage.myclass and try the same I get
Error: Could not find or load main class Reflection
What is exactly happening? And how do I fix it? Thanks.
UPDATE: Thanks everyone. I already had created the directory structure manually. In order for this to work I have to run java com.mypackage.myclass from the root directory, otherwise it will not work. Still don't understand the underlying mechanism. What is exactly happening?

If you have a java class like com.example.test.Main then the compiled class should be in a folder com\example\test and the class file name Main.class
You could manually do it, but compiler has a switch -d <target folder> which will create the folder structure for you.
javac -d . Main.java
In the above command the target folder is the current folder where you have the Java source file.
Once compiled you must use fully qualified name of the class to execute from the same folder you compiled the java file.
java com.example.test.Main
Screen Cap

Related

Java.lang.classnotfoundexception - HelloWorld.class [duplicate]

This question already has answers here:
What does "Could not find or load main class" mean?
(61 answers)
Closed 4 years ago.
Can't seem to get my head around what is causing this error. I have set the CLASSPATH in Environment Variables to C:\Program Files\Java\jdk-10.0.2\bin.
I can compile the code into a .class file using javac HelloWorld.java. However when trying to run the .class file using java HelloWorld, I am getting the below error:
I am running the code from C:\Java which is the directory of both my .java and .class file.
c:\Java>java HelloWorld
Error: Could not find or load main class HelloWorld
Caused by: java.lang.ClassNotFoundException: HelloWorld
CODE:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
Any tips would be greatly appreciated.
The CLASSPATH environment variable is not supposed to point the location of your java installation (you don't really need any environment variable to point at that. A few outdated tools MIGHT need you to set JAVA_HOME, but not to the 'bin' dir but to its parent).
It is supposed to point to the location(s) of your class files.
If your HelloWorld.class file has no package declaration and is located at C:\java\HelloWorld.class, then C:\java needs to be your classpath.
You can use CLASSPATH for this, but... don't. You can have multiple projects on a machine so the notion of 'one machine, one classpath' is silly. Use command line params:
java -cp c:\java HelloWorld
if your classpath,
CLASSPATH=C:\Program Files\Java\jdk-10.0.2\bin
Your class loader will look .class files from there,
include your current directory into your CLASSPATH, where as in your case your .class files are at C:\Java, so the java could not find your .class file, try this one
CLASSPATH=C:\Java
CLASSPATH variable is where java looks for .class and jar file paths
PATH and CLASSPATH

"Error: Could not find or load main class", Using external Jar and multiple classes [duplicate]

This question already has answers here:
Error: Could not find or load main class [duplicate]
(22 answers)
Closed 4 years ago.
I am writing a program that uses three classes and an external .jar library. I got the code to work on my windows machine, on IntelliJ, and am trying to get it running on my Raspberry Pi (Raspbian).
The three classes are called "CommHandler", "SocketHandler" and "ReadAndWrite". ReadAndWrite being the main classes that calls to the others.
The three classes and the .jar library are together in the same directory (home/pi/Final1). I have moved to the directory these are all under and used the code below to compile it all, no errors occur from doing this.
javac -cp jSerialCom-2.0.2.jar *.java
The problem occurs when it comes to running the file, I have been using this to try and run it
java -cp jSerialCom-2.0.2.jar ReadAndWrite
This returns the error message as mentioned in the title
Error: Could not find or load main class ReadAndWrite
I have tried explicitly stating the directory to get to the ReadAndWrite file all resulting in the same error message. (Shown below, tried with both "/" and ".")
java -cp jSerialCom-2.0.2.jar home/pi/Final1/ReadAndWrite
If you have any idea how why this may be happening/how to solve it that would be great.
Thanks
P.S. I have looked at other questions but cant seem to solve my problem from them
e.g.1 - Error: Could not find or load main class
e.g.2 - Java command line with external .jar
java -cp overwrites the classpath. Add the current directory to the classpath so java can find the newly created class:
java -cp jSerialCom-2.0.2.jar:. ReadAndWrite

How to set the output files when compiling with javac [duplicate]

This question already has answers here:
How to compile a java project with a terminal/cmd
(4 answers)
Closed 6 years ago.
This post was edited and submitted for review 1 year ago and failed to reopen the post:
Original close reason(s) were not resolved
My previous question asked how to compile files with the command JAVAC. I still don't know how to set the output files of the compiled source files.
The output of javac is always classfiles, and the name of the file matches the name of the class contained within it. (A source file with multiple classes in will result in multiple output files.)
If you use the -d command line option, javac will also use the package of the class to generate a directory hierarchy, with the -d option specifying the root. Otherwise, each class file will be output in the same directory as the source file from which it was compiled.
From man javac on my system:
-d directory
Sets the destination directory for class files. The destination directory must already exist; javac will not create the destination directory. If a class is part of a package, javac puts the class file in a subdirectory reflecting the package name, creating directories as needed. For example, if you specify -d /home/myclasses and the class is called com.mypackage.MyClass, then the class file is called /home/myclasses/com/mypackage/MyClass.class. If -d is not specified, javac puts the class file in the same directory as the source file. Note: The directory specified by -d is not automatically added to your user class path.
Hope that helps.

Running a java program with multiple jar files and classes [duplicate]

This question already has answers here:
What does "Could not find or load main class" mean?
(61 answers)
Closed 4 years ago.
I am compiling a program with multiple jar files (inside the lib folder) and classes (inside the src/com folder) with:
javac -classpath lib/\* src/com/*.java
I typed this to run the program:
java -cp lib/\* src/com/okc
But it doesn't work. Instead, I get this:
Error: Could not find or load main class src.com.okc
okc.java is the class containing the main method. How can I run a java program with multiple jar files and classes?
A Java class file is not just the file itself. The directory structure which represents a class's package is part of the class file. Your classpath needs to point to the directory which is the parent of the topmost package directory.
Assuming your class is declared with package com;, the topmost package directory is com. So you need the parent of com in your classpath:
java -classpath src:lib/\* com.okc
If your class does not contain any package statement, and you just happened to put it in a com directory, then it belongs to the null package, whose parent directory is com itself:
java -classpath src/com:lib/\* okc
An additional note: It is Java convention to have class names, and their respective file names, start with an uppercase letter. One reason is that it makes class names easy to distinguish from package components.
Try:
java -cp ../lib/\* com.okc
from the src directory (not sure...)
Assuming that your current directory has your lib/ :
java -cp lib src.com.okc

Error executing Java program : Could not find or load main class [duplicate]

This question already has answers here:
What does "Could not find or load main class" mean?
(61 answers)
Closed 8 years ago.
I am taking a Udemy class and I am stuck on the first lesson, getting command prompt to write "Hello world" through java. My error is once I have compiled it, and it creates the class file, and I try to run it through "java HelloWorld" it doesn't run. The error is.
Error: Could not find or load main class HelloWorld
And I can't figure it out, Here is a screenshot showing everything I have: https://app.box.com/s/4heybbazxswm4otjazrw
I have looked through my class discussion and no one had the error, and no StackOverflow topics seemed to be the same problem.
Your CLASSPATH isn't set (or isn't set correctly). From the Java tutorial,
The CLASSPATH variable is one way to tell applications, including the JDK tools, where to look for user classes.
However, you can also specify a CLASSPATH to the java runtime with the -cp argument (also aliased to -classpath. A colon separated list of folders, and jar/zip files to search for class files.
That's why
java -cp . HelloWorld
Allowed the JRE to find HelloWorld.class. You could also set CLASSPATH.

Categories

Resources