Reputation: 41
I know this question has been asked loads of times before, but I'm a rookie programmer and despite trying many of the solutions on this site I still can't fix this issue. I'll be really thankful if you can take the time to figure out what I've done wrong.
Operating system: Windows 8
Java version: 1.8.0 update 25
The command prompt I'm using is the one that comes with Windows. (I'm presuming there are other types so I'm just making it clearer.) The code's a really basic one.
package com.thefinshark.intro;
public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome.");
}
}
So, first I changed the directory to C:\javawork
, where Welcome.java is saved. I set the path to C:\Program Files\Java\jdk1.8.0_25\bin
, then compiled the code. The compilation seemed fine, I found the Welcome.class file in the C:\javawork
as well. The execution, however, kept returning "Could not find or load main class Welcome". I've tried C:\javawork>java Welcome
and C:\javawork>java com.thefinshark.intro.Welcome
, and loads of other variations. I've also changed the classpath to C:\
and C:\javawork
but it still dosen't work. Someone answering a similar question suggested adding dt.jar and tools.jar to the classpath but no dice.
It'll be great if someone could help, and I'll be happy to help pass on the information to the others who have problems like this as well. (As I'm typing this I'm looking at a whole long list of similar questions.)
Upvotes: 1
Views: 2656
Reputation: 206816
The directory structure must match the package name of your source file. So, if your class is in the package com.thefinshark.intro
, then your source file must be in a directory com\thefinshark\intro
.
So, for example, you should save your source file as C:\javawork\com\thefinshark\intro\Welcome.java
, and then compile and run it from the directory C:\javawork
:
C:\javawork> javac com\thefinshark\intro\Welcome.java
C:\javawork> java com.thefinshark.intro.Welcome
Note: The javac
command expects a filename of the source file you are compiling (com\thefinshark\intro\Welcome.java
), and the java
command expects a fully-qualified class name (com.thefinshark.intro.Welcome
).
See Lesson: Packages for more details on how to work with packages.
Upvotes: 4