I copied a simple java app from a book, but am getting a missing class error.
This is the app:
# cat MyFirstApp.java
public class MyFirstApp {
public static void main (String[] args) {
System.out.println("I Rule!");
System.out.println("The World");
}
}
It compiles:
[root#dev hfjava]# javac MyFirstApp.java
[root#dev hfjava]#
But when I go to run the program I get this error:
[root#dev hfjava]# java MyFirstApp.java
Error: Could not find or load main class MyFirstApp.java
What am I doing wrong?
You're doing the javac MyFirstApp.java correctly, but the reason it is not working when you actually try to run the program is because you are typing:
java MyFirstApp.java
when all you have to type is:
java MyFirstApp
Just leave out the .java extension when you actually run the program. Once you do this, your program should execute flawlessly! And remember, as long as you are a programmer, you do rule the world!
Write a comment if this doesn't work and I will continue to try to help you.
Try java -cp . MyFirstApp as your command line.
Try running java MyFirstApp in your command line after running the javac command.
Related
I'm trying to write Java programs in OOP style, and putting two files under the same folder called utility. Also the package is utility.
HelloWorld.java
package utility;
class HelloWorld {
public void hello() {
System.out.println("Hello, world!");
}
}
HelloWorldDriver.java
package utility;
public class HelloWorldDriver {
public static void main(String[] args) {
HelloWorld sayhello = new HelloWorld();
sayhello.hello();
}
}
I used Eclipse to run the HelloWorldDriver.java, which turned out a successful result.
Hello, world!
However, as I was using terminal to run the programs, problems occurred. First, I use javac to compile all the .java files in the folder. Then, I run the file directly.
bash-3.2$ javac *.java
bash-3.2$ java HelloWorldDriver.java
However, the below problems occured.
HelloWorldDriver.java:5: error: cannot find symbol
HelloWorld sayhello = new HelloWorld();
^
symbol: class HelloWorld
location: class HelloWorldDriver
HelloWorldDriver.java:5: error: cannot find symbol
HelloWorld sayhello = new HelloWorld();
^
symbol: class HelloWorld
location: class HelloWorldDriver
2 errors
error: compilation failed
Please help suggest if there's anything wrong about the code or the way I use command lines.
Two things here:
Your error is coming from the way you are trying to run your code in the command line. When you use the java command, you want to specify the compiled class file to run, instead of the java file to run. This would look something like java myProgram.class which is equivalent to java myProgram. Since you are referencing another file in your driver file, you need to add that to the class path so the Java runtime environment knows what to do when you ask it to run functions from that file. In your case, I believe the correct command to run would be java HelloWorldDriver -cp HelloWorld.
Another thing I'd like to mention is that inside your actual code, you don't actually need to create an instance of the HelloWorld class by doing HelloWorld sayhello = new HelloWorld();. Instead you could remove that line and change the line following it to HelloWorld.hello(). There are some instances where you would not want to do this, however for what you are doing, using HelloWorld.hello() will work just fine.
Hello stackoverflow community!
I am in the beginning of my journey of becoming a programmer and currently in the process of learning Java. I have strictly been using Eclipse to compile my programs. However, when I try to run the program through the command line I get:
"Error: Could not find or load main class FirstProg."
I've read through some other discussions on the forum and experimented with different methods, but I cannot get it to execute the program.
The path to my program (FirstProg.java) is as follows: C:\Users\smj7v\workspace\LearningJava\src\com\smj\programmingByDoing
When I enter "javac FirstProg.java" in the CMD it compiles the program and I can see the FirstProg.class generated in the path folder, but when I try to execute, "java FirstProg," it throws the error.
I tried doing things like "java com.smj.programmingByDoing.FirstProg" along with other variations but so far nothing has worked. Obviously I am doing something wrong. Please help!
public class FirstProg {
public static void main(String[] args) {
System.out.println("Mr. Mitchell is cool.");
}
}
The program runs fine in Eclipse btw.
here's a sample way of doing
create following class MyTest.java under c:\com\test folder
package com.test;
public class MyTest
{
public static void main(String[] args)
{
System.out.println("test fle");
}
}
now when you are compiling make sure that you use option -d
run following
cd \com\test
javac -d . mytest.java
next from same folder (com\test),
java com.test.MyTest
Step 1: Write Java Program.
Step 2: Compile java file to class file and generate byte code.
Step 3: Byte code translate to machine code and run on JVM.
Steps to write, compile and run java program using command prompt.
(i). Save the program. After using a text editor, such as NotePad, to create your Java program, save the program with a .java extension.
(ii). Open Command Prompt.
(iii). Navigate to the correct folder.
(iv). Set path.
(v). Compile the program.
Example:javac JavaClassName.java
(vi). Run the program.
Example:java JavaClassName
Visit the good blog to read all steps with example and images:
https://javatutorialdetails.blogspot.in/2017/10/how-java-program-work-step-by-step-in.html
Run your class after setting classpath:
set classpath=%classpath%;.;
java com.smj.programmingByDoing.FirstProg
C:\Users\smj7v\workspace\LearningJava\src> javac com\smj\programmingByDoing\FirstProg.java
C:\Users\smj7v\workspace\LearningJava\src> set classpath=%classpath%;.;
C:\Users\smj7v\workspace\LearningJava\src> java com.smj.programmingByDoing.FirstProg
The principal class XXXX has not been found
The code constructs perfectly but when I try to execute it I get the same error again.
I type the following "Hello world" code:
public class Hello {
public static void main(String[] args) {
System.out.println("Hola mundo");
}
}
I get the same error.
It must be a problem of my program ,I use GEANY because my teacher told me that I can't use netbean or similar.
How do you compile and execute your code? Is it some IDE (Eclipse) or just command line?
IDE should take care about this issue. In command line you have to include all classes into the classpath. Sometime it is confusing because when you compile your code mutually is in classpath but you have to mention it litreally to run.
Here is example
javac Hello.java
compiles Hello.java source to the Hello.class code to the current location. Literally ./Hello.class
To run you should mention this location
java -classpath . Hello
without -classpath . java doesn't know where your class is.
I have recently installed canopy (i don't know if this has anything to do with java)
i have set to system variables the path to javac and java
i was writing compiling and running java programs normally until now that it keeps telling me it can not find or load main class.class
example of code:
class Hello {
public static void main(String[] args) {
System.out.println("Hello");
}
}
what is happening?
ok i changed hello to Hello
i run at powershell:
javac Hello.java
java Hello.class ( i am in the directory of the file)
Error: Could not find or load main class Hello.class
You need to run your program using
java Hello
and omit the .class extension
That should compile fine which Java compiler are you using? Make sure to always save and compile your program before running it.
I have problem running a basic helloworld application in Java form my command-line in widnows 7. I can run it in Java.
Here is my code(in NetBeans):
package helloworld;
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
I have set C:\Program Files\Java\jdk1.8.0_20\bin; on my PATH variable in the Windows Environment.
When running :
javac HelloWorld.java
the HelloWorld.class is built successfully.
However in the next step when I run:
java HelloWorld
I get he following error:
Error: Could not find or load main class HelloWorld
Under my program source root directory I can see these two file:
. HelloWorld.class
. HelloWorld.java
What am I missing please?
You should specify fully qualified class name. That is, you need to run it like that: java helloworld.HelloWorld.
what you need to do is as you have
package helloworld;
and you are trying to execute it from the commandline
do the following steps
First open the terminal or cmd and browse to the folder helloworld.
Example if your helloworld folder in in f:/helloworld open the terminal and browse upto f:/(don't go inside helloworld)
then compile the class as javac helloworld/HelloWorld.java
and try executing the class as java helloworld.HelloWorld
the class name was not fully qualified, try java helloworld.HelloWorld
the .classfile should not be in the directory the java command is run from.
you forgot the helloworld package. So you have to enter java helloworld.HelloWorld to make it work. Next time please use a different package name than the java file, so there is no confusion. :)