Could not find or load main class Java Error Notepad++ - java

I've implemented "Java Compile" and "Compile and Run" (the second as Java Compile and Run) as described in this answer: Java compile and run using notepad++ and nppexec.
Note: I'm using 1.8.0_20 rather than 1.7.0 as described in the answer.
However, when I try to run HelloWorld (as found here: http://docs.oracle.com/javase/8/javafx/get-started-tutorial/hello_world.htm) to test the compiler, I get the following response, including a "Could not find or load main class" error:
NPP_EXEC: "Java Compile and Run"
CD: C:\Users\Bova\Documents
Current directory: C:\Users\Bova\Documents
"C:\Program Files (x86)\Java\jdk1.8.0_20\bin\java" -classpath "C:\Users\Bova\Documents" "HelloWorld"
Process started >>>
Error: Could not find or load main class HelloWorld
<<< Process finished. (Exit code 1)
================ READY ================
What do I need to change to avoid this error?

I had this problem in Notepad++ too. What I did to fix it was I went to the Plugins menu, clicked on NppExec, and selected Follow $(CURRENT_DIRECTORY). After that I can run programs just fine.

The very first line of HelloWorld.java reads:
package helloworld;
Java packages are mapped to directories on the filesystem, so the interpreter expects to find HelloWorld.class inside a helloworld directory. Move the .class file to a helloworld subdir and run it as:
> java helloworld.HelloWorld
from the parent directory (i.e. the directory which contains helloworld/).

Related

unable to compile and run a java program with multiple source files on the command line

I am trying to compile and run a java program that has 3 source files, and I am not able to get it to run.
I have a folder with 3 java source files, Tester.java, CommandHandler.java, and DualList.java. Tester.java contains the main function, and depends on CommandHandler.java, which depends on DualList.java
I tried to compile the program with javac Tester.java CommandHandler.java DualList.java. It created the three class files, but when I try to run the program with java -cp . Tester I get the following error:
Error: Could not find or load main class Tester
Caused by: java.lang.NoClassDefFoundError: cmsc420_s22/Tester (wrong name: Tester)
I can see that the class file is there, and I got no errors during compilation, so I'm not sure what is going on. I think it might be a problem with my java installation or my configuration. I'm doing this on a kali linux WSL distribution. I haven't changed anything about my installation, and I set my classpath to the current folder so I'm not sure what's wrong
Thanks

Cannot run Java app on cmd using Java11 and Windows

I ahve tried several approaches as on How do I run a Java program from the command line on Windows? and create exactly the same class and packages to use the same things.
Here is the sample class located on C:\SimpleJavaProject\src\com\hello\programs:
package com.hello.programs;
public class ABC {
public static void main(String[] args) {
System.out.println("Hello world");
}
}
Then I compile it in the usual way:
C:\SimpleJavaProject\src\com\hello\programs > javac ABC.java
Later, run it by giving the package name and then my java class name:
C:\SimpleJavaProject\src > java com.hello.programs.ABC
In each time I try to run java app, I get "Error: Could not find or load main class com.hello.programs.ABC" error. Then have a look at What does “Could not find or load main class” mean? page and tried some approaches on that page. But still the same error.
It is too simple, but still I have not managed to run the simple app yet. So, how to fix this problem? And after running the app, how can I pass args on cmd?
Update: I could already generate ABC.class file by running the following command. BUT, I cannot run the app and see the "Hello world" on the console.
cd C:\SimpleJavaProject\src\com\hello\programs
javac ABC.java
--> generates ABC.class in C:\SimpleJavaProject\src\com\hello\programs
java com.hello.programs.ABC
When you run this, java is going to check each and every CLASSPATH path for that + /com/hello/programs/ABC.class, will load that, and then run it.
This must mean that either:
[A] Your classpath does not include the current directory; The fix is java -cp . com.hello.programs.ABC.
[B] you didn't do what you wrote, and e.g. dir com/hello/programs.ABC.class prints nothing.
Note that you're not doing it right; a class file should never be in a directory path that includes src. If you don't want to bother with build tools like maven or gradle, I strongly suggest you don't bother with a src dir then either. If you must, the -d option can be passed to javac to tell it where tou put the file. If you want to separate source and class files, then that should be targeting a directory named bin or build or whatnot (a sibling of the src dir).
When you use javac ABC.java you are compiling the class, and javac places it in the current directory.
So java com.hello.programs.ABC would not work (because com/hello/programs/ABC.class file does not exists).
You can use the javac -d flag:
-d <directory> Specify where to place generated class files
For instance:
> javac -d . ABC.java
> java com.hello.programs.ABC
Hello world
> cd com\hello\programs
> dir
ABC.class
Would work, because javac did place ABC in com/hello/programs.
Update: For clarity, once you compiled using javac -d . ABC.java you can run it using java com.hello.programs.ABC and you should see Hello world in the screen.

Unable to run compiled Java code - could not find or load main class

I am new to Java, and having problems running my compiled code.
I have a file called AdditionApplication. I compile this line:
javac -cp * AdditionApplication.java
This produces a file in the current directory called AdditionApplication.class
When I try and run the program with the code:
java -cp * AdditionApplication
It gives the error Error: Could not find or load main class AdditionApplication
What could be a cause of such behavior?
UPDATE
So my code requires a jar file to be included in the initial compilation of the file. This seemed to only work if I had javac - cp * AdditionApplication.java, or if I specified the full path to the jar file.
When I switch over to us a . in 'java -cp . AdditionApplication', I get the error NoClassDefFoundError - which I gather occurs when the class which was initial reference during compilation is no longer available - why would that be the case?
The classpath should be the directory with the files (not * unless they're jar files). Assuming you have a class file (and based on your other command you do), you add that folder to the classpath. Something like,
java -cp . AdditionApplication
Since you have a jar as well, you could do (on *NIX systems)
java -cp .:* AdditionApplication
or on Windows
java -cp .;* AdditionApplication

"java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver" error when running from terminal

I have a program that I run from Eclipse successfully.
However, when I want to run it from terminal, I encounter the famous error:
"java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver"
on this line:
Class drvClass = Class.forName("oracle.jdbc.driver.OracleDriver");
PS:
I have the following in CLASSPATH:
/oracle/jdbc/lib/ojdbc6.jar
Also note that I compile it successfully (javac Test2.java). Then when I run it (java Test2), I get the following error:
Error: Could not find or load main class Test2
So I run:
java -classpath ~/Desktop/JDBC2/src Test2
It runs, but I get the above "ClassNotFoundException" though.
I found this question tricky: the reason is related to semicolon after jar file address.
At first I changed the directory of MySample.java to another directory (you can don't do that) like C:\
then I removed package address from the source code, at the end I run this command in cmd
java -cp path_to_oracle_driver.jar; MySample
P.S. If you want run it from terminal you have to remove package PackageAddress from the source code and compile it again.
As #yngwietiger mentioned above in the comments, using -classpath parameter when running the .class file, overrides the original CLASSPATH and the predefined ojdbc6.jar file. So we need to mention both when running:
java -classpath ~/Desktop/JDBC2/src:/oracle/jdbc/lib/ojdbc6.jar Test2
Or, as a better solution, we can add the current path to CLASSPATH (note the colon and dot at the end):
export CLASSPATH=$CLASSPATH:.
And, in order to run, we just need to type:
Java Test2

could not find or load main class - Error when run through Command prompt

I know there are similar questions posted before and I went through those but still its not working for me.
I am creating a sample JMS test class (chat application) and uses javaee.jar and javax.jms.jar. I can test it through Eclipse IDE and it works fine. But I am trying to run through command prompt so I can run multiple windows.I managed to compile the Chat.java file and it created the Chat.class. But when i try to run it, I get could not find or load main class. These are the commands I used:
From the src/domain folder:
javac -classpath javaee.jar;javax.jms.jar Chat.java---- this created Chat.Class inside domain folder where domain is the package name
The I ran the following command from src folder
java -classpath javaee.jar;javax.jms.jar domain.Chat ---- this gives me the could not find or load main class domain.Chat error message
But when I run without the -classpath parameter(java domain.Chat), it reads the main() and gives me different error since it cant find the jms jar files.
E:\eclipse\Spring\JMSChat\src>java domain.Chat
Exception in thread "main" java.lang.NoClassDefFoundError: javax/jms/MessageList
ener
So basically it finds the Chat.class file when I don't pass in the classpath parameter and it cannot find the class when I use the classpath to add the jars. I tried running it from within domain folder as well as from src folder, but no luck. Any clue what I am doing wrong?
Thanks in advance.
Try this
java -classpath javaee.jar;javax.jms.jar;. domain.Chat
By default java uses the current directory in the classpath. When you use the -cp flag, it does not so the path to domain.Chat is not found.
1) You can run multiple instances of the app from Eclipse, and you can check their outputs by cycling through their allocated consoles by clicking on the arrow next to the console icon
2) try running from your source folder java -classpath .;javaee.jar;javax.jms.jar domain.Chat "." means current dir

Categories

Resources