I just realized I'm about to graduate and I still don't know how to handle this situation:
Say I have a java package named mystuff.project1 and inside the package I have Project1.java (which has the main method) and ThingThatDoesStuff.java . Both are public classes, have the package declaration at the top of the file, etc. I can debug this project fine in Eclipse.
For maximum simplicity, I move the project to C:\ so the java files are located in c:\mystuff\project1
I navigate into c:\mystuff\project1 and type javac *.java to compile the class files
Now how the blazes do I run my application?
java Project1 doesn't work
java mystuff.project1.Project1 doesn't work
java -cp . Project1 doesn't work
java -cp . mystuff.project1.Project1 doesn't work
All of the above give me "Error: Could not find or load main class"
I've been searching SO and elsewhere to try to understand this problem but I'm completely baffled.
I've:
cleared my CLASSPATH variable
triple-checked PATH etc
successfully tried compiling and running standalone class files that aren't in a package
I understand this is a stupid newbie question but I just can't figure it out. I realized every other time I've run into this problem I similarly couldn't find a solution and put all of the classes into a single file. I'd rather learn how to stop doing that now.
I think you have to change to C:\ and then enter:
javac mystuff\project1\*.java
java mystuff\project1\Project1
and this should work.
It's either that or:
javac mystuff/project1/*.java
java mystuff/project1/Project1
Related
I'm currently learning java from a book and I just reached packages. I've been saving all my files on my desktop and compiling/running programs from Mac's Terminal console.
John-MacBook-Pro:~ john$ cd desktop
John-MacBook-Pro:desktop john$ javac Learning.java.
John-MacBook-Pro:desktop john$ java Learning
.... program executes and so on .....
Now I save my .java files into a package (create a new folder). Let's call the package 'book' And I'm told to run programs like this now:
javac book/Learning.java
java book.Learning
This works when I have one folder, sure, but when subclasses and more packages are added into that book folder how do I compile things deeper in? Not to mention how to run them afterwards?
The book might have assume prior knowledge so it just dives right in and tells me to setup CLASSPATH or use -classpath on my Macbook before attempting. I've tried various commands on terminal and it seems to compile sometimes where I have to manually change directory to open each folder (which is a lot of typed commands). Trying to run any classes always result in class not found. Every other answer seems to have some of the basic stuff setup already or is explained in terminology I don't understand yet.
When more classes are added, you compile them all:
javac book/Learning.java book/chapter/Chapter.java ...
You run the main class exactly the same way:
java book.Learning
If you're not in the package where the root of the package tree is (i.e. your desktop directory), you pass it in the classpath:
java -classpath /users/Leosam/desktop book.Learning
Note that it works on macOS the same way as on any other platform.
I'm having a problem where the java command - no matter what I'm trying to run, says that it Could not find or load main class.
Everything is fine when compiling with javac, .class files are created. So when I run:
javac HelloWorld.java
on
public class HelloWorld{
public static void main(String[] args){
System.out.println("Hello World");
}
}
everything compiles fine, a HelloWorld.class file is created along side the HelloWorld.java file. However when I then go to run:
java HelloWorld
1) the most telling sign is that when I press Tab to autofill HelloWorld nothing comes up.
2) when I do run it, I get the Error: Could not find or load main class HelloWorld despite it being in the same directory, not being part of a package, compiling fine with a .class file, the program having a main class.
For reference running Fedora 23 64bit, openjdk version "1.8.0_111".
Just a small reminder for the newbies in Java:
When compiling, you type:
javac MyClass.java
Now, you've got two files:
MyClass.class MyClass.java
Now, whereas you typed the .java extension when compiling, you must NOT type the extension .class when running the program. You should just type:
java MyClass
If you type java MyClass.class then you'll get:
Error: Could not find or load main class
Try using java -cp . HelloWorld
Some good reading: http://www.sergiy.ca/how-to-compile-and-launch-java-code-from-command-line/
You need to specify classpath parameter while running your example:
java -cp . HelloWorld
java -cp HelloWorld
works. I use windows 10, and was checking out the very course. I first had to add it to path, and spent time wasted on it. Be sure, however, to NOT include the .class part of the name. Java is not my first language, but Java is portable and is a suitable language for everyone.
The same happened to me while compiling a piece of code (that was initially writen for an IDE with several files) throug terminal. The problem was mentioning the package with the same name of the main class (package HelloWorld). I fixed it and now it works. Not sure if that's your case
May be you have removed your JDK From System.
You can change it using following steps
1> Select Project
2> Right Click On Project
3> Click On Properties
4> Go to Java Build Path
5> Click On Libraries Option
6> Select JRE System Library
7> Click On Edit
8> Change Your Library Accordingly
I am trying to run a java program through the Terminal on Mac, yet getting:
Error: Could not find or load main class (MY CLASSNAME)
I compiled this application with Eclipse, and when I run this with Eclipse, it works fine.
Furthermore, I am in the right directory, as when I type "ls" in the Terminal, it lists all the files, includes the class file I am trying to run.
This is what I type:
java mainClass
I would very much appreciate help to solve this!
Thank you,
Dean
EDIT: Solution - instead of java mainClass, it must have package too: java startPackage.mainClass
Start by making sure you are at the directory above the top level package
If the class belongs to the package com.foo.bar, you want to be in the directory above com.
In your case, you want to be in the directory above startPack.
Then you need to use the fully qualified name to run the class...
java statPack.mainClass
For example...
Make sure you have the current directory inside your CLASSPATH.
java -cp . mainClass
To set this globally, you can use export CLASSPATH=$CLASSPATH:. inside .bash_profile.
Separately, if your class lives inside a package such as com.foo.bar, then you will need to go to the parent directory of com and run your application with the full path.
java com.foo.bar.mainClass
I too faced this on Mac machine and then what I had to do to make it work was:
Problem Statement:
I had one package xyz under the root of project i.e src/main/java and then inside xyz package I had one class Student.java
my current directory is /Users/username/projectname/src/main/java/xyz:
I can see Student.java exists here
and I compiled it using javac Student.java
Now I see class file has been created at this location. But when I try to run the class file using java Student
I get the error: Error: Could not find or load main class Student
Solution:
Now the solution is to go one step back in the directory and go to root path:/Users/username/projectname/src/main/java and run the command
java xyz.Student
and it will work.
Link to follow: https://javarevisited.blogspot.com/2015/04/error-could-not-find-or-load-main-class-helloworld-java.html
For people dumb like me, make sure you are typing java HelloWorld - and NOT java HelloWorld.class - to run the compiled file with the name HelloWorld.class. This is especially so if you are used to hitting the tab key to complete the file name, as the terminal will give you java HelloWorld.class if you hit the tab key for autocomplete after typing something like java He...
This answer is here because it took 3 sites, including this answer, and 25 mintues before I figured out what I was doing wrong.
Logic is easy, typing is hard.
Using the absolute path can also resolve this problem:
java -classpath /Users/xingliu/IdeaProjects/springproject/src/main/java/ startPackage.mainClass
I understand there are a lot of threads similar to this one, but I couldn't find the one that solved my problem. Following this instruction I was able to get java in terminal and be able to compile. I am able to "javac main.java" with no errors, but when I "java main.java", it simply says it could not find or load main class main.java. I believe that my classpath is wrong but i'm not entirely sure how to fix this either. This is what comes out when I type in echo $PATH
/usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/home/taka/.rvm/bin
and when I type echo $CLASSPATH it doesn't show anything.
I have also tried java -cp ./ main.java as that seemed to have worked when I compiled and ran HelloWorld.java
If your javac is successful then update your classpath environment variable and add current directory i.e. . in the classpath, then run the java as below:
java main
Please note: There is no .java extension as you need to run .class file(which was generated after javac) that also without mentioning the extension. Java uses generated class files to execute not the original source files.
main.java java is your source code . you cant run java source without compile. For compilation you should use javac command. After that it will create a main.class file which can understand by interpreter which is java.
So you to run your class use java main or java main.class
I have just copied Key-Listener code from http://java.sun.com/docs/books/tutorial/uiswing/examples/events/KeyEventDemoProject/src/events/KeyEventDemo.java. I was able to compalie it with the "javac" command. But when I try to execute the compiled code (typing "java KeyEventDemo") I have a large message in the end of which I see:
Could not find the main class: KeyEventDemo. Program will exit.
Yesterday I had a similar problem on Windows Vista (now I am on Ubuntu). In the Windows I was able to solve the problem by typing "java -cp . ProgramName" or alternatively by adding new values ("." and "..")to the environment variable "classpath".
On Ubuntu the first solution does not work. I mean, when I type "java -cp . KeyEventDemo" I still have the problem. Moreover, on Ubuntu I was able to run other programs just typing "java ProgramName".
So, can anybody tell me what is special about this KeyEventDemo? Why it does not wont to work and how it can be solved?
The class KeyEventDemo is in a package events To run it, you must be in the parent folder of the events folder that contains the class, and run it using its fully qualified name, including the package:
java events.KeyEventDemo
The classpath must contain the folder (or JAR) that's the root of the folder hierarchy that represents the packages; the current folder is (I believe) included automatically.
This program is not in the default package, but in the package "events": use java -cp . events.KeyEventDemo from the directory containing the folder "events":
+work
+events
-KeyEventDemo.class
It is because the KeyEvent class is in package events.
You either have to remove the package events; line from source code, or compile it with:
javac -d . KeyEventDemo.java
Perhaps you compile and run with diferent java version.
This is common when you try to execute an example at eclipse.