It's Java class loader who looked into CLASSPATH for loading classes.Ĭoming back to the problem in hand, if you are a beginner in Java, who are able to run the program from Eclipse but getting "Error: Could not find or load main class HelloWorld" when trying to run the same program from the command line then follow the steps given here to solve it. While, CLASSPATH is used to locate the class file or JAR files. It is also used to locate native libraries used by your Java program. This way you always know which JARs are included in your classpath.įor beginners, another important thing to understand is the difference between PATH and CLASSPATH, you must know that PATH is used locate system executable, commands or. Instead, I use -cp or -classpath option with java command to run my program.
Personally, I don't like this environment variable and doesn't define in my environment variable, because its confusing and source of so many classpath related issue. In Eclipse, it's easy to compile and run the program because Eclipse takes care of all Classpath setup, but when you run your Java program from command line, CLASSPATH environment variable comes in picture. Since many Java programmer is now started programming using Eclipse they face this issue when they first try to run their Java program from command line. For now, you just remember that there is an environment variable called CLASSPATH which includes directories where Java looks for all class files and if it doesn't find your main class there then it throws "Error: Could not find or load main class XXX", where XXX is the name of your main class.
For the curious reader, I would suggest reading my post How Classpath works in Java, a must read for a beginner. In order to solve this error, you must know how Java find and loads the classes, that's a little bit complex topic for beginners, but we will touch the same base here. : : String index out of range: -1Īt .raiseEvent2(BA.java:120)Īt .raiseEvent(BA.java:78)Īt $n(JServlet.java:130)Īt .Handle(JServlet.java:110)Īt .doGet(JServlet.java:84)Īt .service(HttpServlet.java:687)Īt .service(HttpServlet.java:790)Īt .ServletHolder.handle(ServletHolder.java:865)Īt .ServletHandler.doHandle(ServletHandler.java:535)Īt .(ScopedHandler.java:255)Īt .(SessionHandler.java:1595)Īt .(ContextHandler.java:1253)Īt .(ScopedHandler.java:203)Īt .ServletHandler.doScope(ServletHandler.java:473)Īt .(SessionHandler.java:1564)Īt .(ScopedHandler.java:201)Īt .(ContextHandler.java:1155)Īt .(ScopedHandler.java:144)Īt .(HandlerCollection.java:126)Īt .(HandlerWrapper.java:132)Īt .Server.handle(Server.java:531)Īt .HttpChannel.handle(HttpChannel.java:352)Īt .HttpConnection.onFillable(HttpConnection.java:260)Īt .AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:281)Īt .FillInterest.fillable(FillInterest.java:102)Īt .(SslConnection.java:291)Īt .ssl.SslConnection$3.succeeded(SslConnection.java:151)Īt .ChannelEndPoint$2.run(ChannelEndPoint.java:118)Īt .(EatWhatYouKill.java:333)Īt .(EatWhatYouKill.java:310)Īt .(EatWhatYouKill.java:168)Īt .(EatWhatYouKill.java:126)Īt .thread.ReservedThreadExecutor$n(ReservedThreadExecutor.java:366)Īt .(QueuedThreadPool.java:760)Īt .thread.QueuedThreadPool$2.run(QueuedThreadPool.java:678)Ĭaused by: : String index out of range: -1Īt (String.java:1967)Īt ._handle(download.java:50)Īt 0(Native Method)Īt (NativeMethodAccessorImpl.java:62)Īt (DelegatingMethodAccessorImpl.java:43)Īt .invoke(Method.java:498)Īt .raiseEvent2(BA.Error: Could not find or load main class HelloWorld comes when you are trying to run your Java program using java command with the main class as HelloWorld but Java is not able to find the class. 10:24:49.013:INFO ejs.Server:main: Started server is readyġ2:53:25| Compilation time: 8844,, queue: 0, success: true 10:24:48.470:INFO ejsh.ContextHandler:main: Started 10:24:48.479:INFO ejs.AbstractNCSARequestLog:main: Opened /Users/leonludwig/Desktop/BuildServerNEW/logs/b4j-2019_01_18.request.log 10:24:48.448:INFO ejs.session:main: No SessionScavenger set, using defaults 10:24:48.448:INFO ejs.session:main: DefaultSessionIdManager workerName=node0
Xcode path: /Applications/Xcode.app/Contents/Developer 10:24:46.038:INFO::main: Logging initialized to .log.StdErrLogĪppPath=/Users/leonludwig/Desktop/BuildServerNEW Mac:BuildServerNEW leonludwig$ java -jar B4iBuildServer.jar