What to do if you cannot execute with the command "Java package name / class name"

Overview

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.

environment

About the file to be executed

"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");
  	}
}

Cause

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.

Description

① Current directory setting

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$

② Compile

HIrokinoMacBook-Pro:Chapter6 user$ javac hoge/Main.java
HIrokinoMacBook-Pro:Chapter6 user$ 

③ Execute with 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

④ Check the current classpath

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

⑤ Execute with 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.

⑥ Delete the classpath

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

⑦ Execute with 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

What to do if you cannot execute with the command "Java package name / class name"
What to do if the adb command cannot be executed
What to do if you can't use the rails command
What to do if you cannot roll back the migration (UnknownMigrationVersionError)
What to do if Operation not permitted is displayed when you execute a command in the terminal
What to do if you change the Listen Address from the settings screen with Mattermost docker
What to do if you installed Ruby with rbenv but the version does not change
[Rails] What to do if you can't get an error message with the errors method
What to do if validation doesn't work with the update action
What to do if you forget the root password in CentOS7
[Rails] What to do if you can't get parameters with form_with
What to do if you install Ubuntu
What to do if JavaMail cannot be used with OpenJDK 11 or later
[Composer] [Laravel] What to do if you cannot install due to insufficient memory
What to do if you can't install the plugin from the Eclipse marketplace
What to do if you don't like the code generated by swagger-codegen-cli
What to do if you push incorrect information
What to do if you get Application with name appName is already registered. When you try to start GlassFish
What to do if you get the error Too long with no output (exceeded 10m0s) on CircleCI
What to do if you can't activate the select box created by bootstrap-select
What to do if you get the error message unrecognized selector send to instance "***"
What to do if you have installed Java for OS X on macOS
What to do if you get To install the missing version, run `gem install bundler: 2.1.4`
What to do if the build fails with the error "Module compiled with Swift x.x.x cannot be imported by the Swift x.x.x compiler"
What to do if you have enabled Use the WSL2 based engine in Docker Desktop with insufficient WSL2 installation
How to translate the error message into Japanese (What to do if you cannot log in for some reason)
What to do if the Rails server can't start
What to do if you accidentally create a model
What to do if you get a NoClassDefFoundError when trying to run eclipse on Java9
What to do if you get the error Couldn't find Item without an ID
What to do if the JSONHint annotation does not work with lombok and JSONIC
What to do if you hit the wall of Too many symbol files when uploading with App Store Connect
[Java] What to do if the contents saved in the DB and the name of the enum are different in the enum that reflects the DB definition
[Solution] What to do if you get a Docker error'ERROR: Cannot connect to the Docker daemon at unix: ///var/run/docker.sock. Is the docker daemon running?'
How to get the class name / method name running in Java
What to do if you get a java.io.IOException in GlassFish
What to do when you launch an application with rails
# What to do if you accidentally do rails db: migrate: drop
What to do if you don't see the test code error message in the terminal console
[Rails] What to do if you accidentally install bundle in the production environment in your local environment
[Rails Tutorial Chapter 2] What to do when you make a mistake in the column name
What to do if you get a "Cannot Pull Container Error" when starting ECS ​​Fargate
[Ubuntu 20.04] What to do if the external monitor is not recognized
How to execute Postgresql copy command with column information on Java
What to do if you get a gcc error in Docker
If you want to include the parent class in Lombok's @builder
If you want to change the Java development environment from Eclipse
What to do if the Rails page doesn't appear in Rails tutorial 1.3.2
What to do if Cloud9 is full in the Rails tutorial
What to do if the Eclipse Maven dependency Jar is wrong
What to do if you get a DISPLAY error in gym.render ()
What to do if you get an error with bundle install by entering gem'bcrypt' in your Gemfile
What to do if you should have installed Rails but an error occurs with rails -v (for beginners)
The first thing to do when you want to be happy with Heroku on GitHub with Eclipse in Java
What to do if you can't bundle update and bundle install after installing Ruby 3.0.0 in the Rails tutorial
[IOS] What to do when the image is filled with one color
What to do if you get a groovy warning in Thymeleaf Layout
What to do if you get an error during rails db: reset
What to do if the background image is not applied after deployment
What to do if the prefix c is not bound in JSP
What to do when CentOS cannot be started with VirtualBox on Catalina