Could not execute with java package name / class name
, whereas
I was wondering what I could do with java -classpath .package / classname
I've found my own workaround on how to run it with java package name / class name
, so I'll post it.
If you have entered java package name / class name
(or java package name.class name
) according to the textbook, but you are having trouble executing it, please try the contents of this article.
"Main.java" (belongs to the "hoge" package)
It is a simple program that displays "hello.java" in the terminal when executed. The storage directory is "/Users/user/src/Java/sukiri1/Chapter6/hoge/Main.java".
Main.java
package hoge;
public class Main {
public static void main(String[] args) {
System.out.println("hello.java");
}
}
I have to execute from the current directory where the class file is stored, but I tried to execute from the path set in the classpath in ".bash_profile" where the corresponding class file is not stored. treasure.
The cause is that the Java VM (Virtual Machine) went looking for the class file in the wrong place.
First, set the current directory to "/ Users / user / src / Java / sukiri1 / Chapter 6" where the "hoge" package is stored.
HIrokinoMacBook-Pro:hoge user$ cd /Users/user/src/Java/sukiri1/Chapter6
HIrokinoMacBook-Pro:Chapter6 user$
HIrokinoMacBook-Pro:Chapter6 user$ javac hoge/Main.java
HIrokinoMacBook-Pro:Chapter6 user$
java hoge / Main
I got an error saying "hoge.Main not found".
HIrokinoMacBook-Pro:Chapter6 user$ java hoge/Main
error:Main class hoge.Could not find and load Main
Cause: java.lang.ClassNotFoundException: hoge.Main
Enter the following command in the terminal to check the current classpath.
$ env | grep CLASSPATH
The path set in "CLASSPATH =" of my ".bash_profile" is displayed. In "/Applications/Java/apache-tomcat-9.0.36/lib/servlet-api.jar", "Hoge / Main.class" required for program execution is not stored.
HIrokinoMacBook-Pro:Chapter6 user$ env | grep CLASSPATH
CLASSPATH=/Applications/Java/apache-tomcat-9.0.36/lib/servlet-api.jar
Reference ↓ The author's ".bash_profile"
export JAVA_HOME="$(/usr/libexec/java_home -v 14.0.1)"
export CATALINA_HOME=/Applications/Java/apache-tomcat-9.0.36
export PATH="/usr/bin:${PATH}:${JAVA_HOME}"
export ANT_HOME=/Users/user/tool/apache-ant-1.10.8
export PATH=${PATH}:${ANT_HOME}/bin
export PATH=${PATH}:/Users/user/tool
export CATALINA_OPTS=-Dfile.encoding=UTF-8
export CLASSPATH=/Applications/Java/apache-tomcat-9.0.36/lib/servlet-api.jar
java -classpath .hoge / Main
To specify from which "location" the Java VM (Virtual Machine) should read the required class files Use the option "-classpath" to specify the current directory where "hoge / Main" is stored. ". (Period)" refers to the current directory.
The option "-classpath" has the ability to set a classpath for each ** application **, Here ** "Run the application" hoge / Main "this time with the" current directory "as the classpath!" ** It is said. Even if you set the classpath in the "CLASSPATH environment variable" described below, you can use this option to The classpath set in the "CLASSPATH environment variable" is invalidated, and the classpath set in the option "-classpath" is set.
About "CLASSPATH environment variable" This is a method to pre-register the classpath in the OS so that you do not have to tell the Java VM the location with "-classpath" at runtime. Registering the classpath in the OS in this way applies to all applications.
When I typed the command "env | grep CLASSPATH" in ④, the path set in "CLASSPATH =" of my ".bash_profile" was displayed, but this is the classpath registered in the OS. Unless otherwise specified by "-classpath", this classpath set in the environment variable is automatically applied, so If you type the command "env | grep CLASSPATH", it will not be in the current directory where the class files are stored. This classpath set in the environment variable is output to the terminal.
For "-classpath" and "CLASSPATH environment variable", refer to the following articles. The top article is very easy to understand. https://www.javaroad.jp/java_basic2.htm https://docs.oracle.com/javase/jp/6/technotes/tools/windows/classpath.html https://style.potepan.com/articles/17670.html https://www.atmarkit.co.jp/fjava/onepoint/java/jv_jcmd.html
It's longer, but when I run it with java -classpath .hoge / Main
, it works fine as below.
HIrokinoMacBook-Pro:Chapter6 user$ java -classpath . hoge/Main
hello.java
Now, I will introduce the method to execute with the command "java package name / class name" from here.
Enter the following command in the terminal to delete the environment variable.
$ export CLASSPATH=
Enter $ env | grep CLASSPATH
again, and ...
HIrokinoMacBook-Pro:Chapter6 user$ env | grep CLASSPATH
CLASSPATH=
I was able to confirm that the classpath was deleted.
reference How to delete the classpath http://javacafebreak.tripod.com/document/cpad_tips/cpad_classpath.html
How to rewrite the CLASSPATH environment variable from the terminal https://style.potepan.com/articles/17670.html
java hoge / Main
HIrokinoMacBook-Pro:Chapter6 user$ java hoge/Main
hello.java
It's a success.
By the way, you can also execute it with java hoge.Main
as follows.
HIrokinoMacBook-Pro:Chapter6 user$ java hoge.Main
hello.java
If no classpath is specified in either "CLASSPATH environment variable" or "-classpath"
The current directory is set as the classpath.
Therefore, even if you enter the command without including java hoge.Main
and the classpath,
The Java VM was able to find the package and class files, so it was able to run.
It's been long, but that's it!
Recommended Posts