Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the problem that the Java error prompt cannot find or load the main class

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you how to solve the Java error prompt can not find or can not load the main class of the problem, I believe that most people do not know much, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to understand it!

Preface

In general, we use tools for code editing and debugging, such as eclipse, Manven, Android Studio, sublime, vim, notepad, notepad, and so on.

When we use eclipse android studio to create project and java class files, both have package names and are compiled and run normally with the tool, but when we switch to command line execution:

Javac xxx.java

Java xxx

It is very likely that the main class cannot be found or cannot be loaded

When we are learning Java, the class file does not set the package name (package), in this case pay attention to classpath, basically no problem. But when we write code with the tool (eclipse,android studio), the location of the code file is under the directory separated by the package name, note that this is the relative physical directory (com/eagle/app), and the package name (package) is declared in the code, which can be considered as a virtual directory (com.eagle.app). This paper mainly explains the cause of the error and the correct way to deal with it when there is no problem with classpath and system environment variable PATH.

The code file is simple as follows:

Package com.eagle.app;public class MainJava {public static void main (String [] args) {if (args! = null) {for (String arg: args) {System.out.println ("arg =" + arg);} System.out.println ("arg =" + args);} else {System.out.println ("args =" + null) }} javac xxx.java compilation requires a relative physical path

As shown in the figure above, take Android studio as an example

1. There is a src directory under the project directory, but it contains the entire app files, not the "src" directory of the code; if eclipse, the src directory is the code directory.

2. This is the "src" directory of the code, and there is a "directory" com/eagle/app that corresponds to our package name one by one.

To compile MainJava, use:

/ / cd to the app directory javac MainJava.java

Or

/ / cd to the eagle directory javac app/MainJava.java

Even

/ / cd to the main directory javac java/com/eagle/app/MainJava.java

It's all okay. Of course, it can also be any location + relative directory.

Back to explain: relative to the currently executed directory (such as app directory, eagle directory, main directory), the physical path is a valid computer path (xxx/xxx/xxx), so it is named relative to the physical path.

If something goes wrong, it must be the wrong path.

Note: the code directory is determined by the starting directory of the package name when the new code file is created.

Virtual path is required for java xxx execution

This is interesting here, to emphasize that the package name virtual path: xxx.xxx.xxx, not a "/" separated directory, so it is called a virtual path.

In the figure above, 2 is the java code and 1 is the bytecode file generated by the tool compilation. Now you need to execute MainJava.class, the correct command:

/ / cd to the main directory in 1, but not to the com or lower directory

F:\ GSProject > cd javatest\ build\ classes\ java\ mainF:\ GSProject\ javatest\ build\ classes\ java\ main > java com.eagle.app.MainJava

Note: do not take. Class (java com.eagle.app.MainJava.class is incorrect), and do not try to change to another directory to be executed with a relative directory, because the java command recognizes the latter parameter as the package name.

The following are all wrong

F:\ GSProject\ javatest\ build\ classes\ java\ main > java com.eagle.app.MainJava.class

Error: main class com.eagle.app.MainJava.class cannot be found or cannot be loaded

F:\ GSProject\ javatest\ build\ classes\ java > java main\ com.eagle.app.MainJava

Error: main class main\ com.eagle.app.MainJava cannot be found or cannot be loaded

OK, now let's move the directory circled in 1 above to the javatest directory, and we will execute java com.eagle.app.MainJava in the javatest directory

F:\ GSProject\ javatest > java com.eagle.app.MainJavaarg = [Ljava.lang.String;@75b84c92

As a result, the parameter address of the main function is output normally. So we can run our bytecode at will, put it on linux or mac, and we can happily do what we want to do.

The directory composition in 1 is package com.eagle.app;. Make sure that the javac file path is * * / xxx/xxx/xxx/Name.java**.

The path to the java file is xxx.xx.x.Name, without class.

The above is all the contents of this article entitled "how to solve the problem that the Java error prompt cannot find or load the main class". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report