Error: Could not find or load main class HelloApp - java

I'm a beginner trying to learn to program in Java. I'm using the book Java All In One for Dummies. The book has me using Textpad to write and compile the code. I've got the following simple program:
public class HelloApp {
public static void main(String[] args) {
printHello("World");
}
public static void printHello(String greetee) {
System.out.println("Hello " + greetee);
}
}
I saved the files for the java application in at the following path. D:\program files\java. When I compile the app with the Textpad compiler, it compiles without any errors. I have the files saved in the following folder: D:\program files\java. When I try to run the app from the Run Java Application command in Textpad, it gives me the error message "Error: Could not find or load main class HelloApp.java". When I run the app from the command prompt using the java HelloApp.java, it runs fine displaying "Hello World" Why does it work when I run it using the java HelloApp.java command which is what I assume the "run java application" command in Textpad does, and not work using the command in Textpad? What am I doing wrong?
I tried resaving and recompiling the application. Then I ran it directly from the command prompt. It worked when running directly from the command prompt but it won't work when trying to run from the "Run Java Application" command in Textpad.
I assumed it would work as it seems like I'm doing the exact same thing from Textpad as I am from using the java HelloApp.java command at the windows command prompt.

Related

Error: Could not find or load main class in Textpad 8

I've been trying to get back into programming and I've been redoing some old labs. I'm setting up Textpad 8 so I can run java applications and it works fine until I add a package statement like so:
package pkg;
public class inPkg{
public static void main(String args[]){
System.out.println("Hello World");
}
}
The file's location: C:\214\pkg\inPkg.java
When I compile everything is fine but when I try to run it, I get this error message:
Error: Could not find or load main class inPkg
Tool completed with exit code 1
Compile Java Tool:
Parameters: javac -classpath "$FileDir;h:\214\;c:\214\;" $File
Run Java Application Tool:
Parameters: java -classpath "$FileDir;h:\214\;c:\214\;" $BaseName
These tools are the only thing I changed in the configuration. The classpath have been written to follow the lab. instructions.
PS. Without the packages statement, the application runs perfectly.
Because probably you are not using the fully qualified class name when you do the java run. use
java -classpath 'your class path' pkg.inPkg
It will compile and execute correctly with following commands
C:\214>javac.exe pkg\inPkg.java
C:\214>java.exe pkg.inPkg
Note that the file location is C:\214\pkg\inPkg.java, however you execute the commands from C:\214

Having Trouble Running Java Program in CMD

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

How to run a Java project from command prompt

I have source code for QR code generation using Java.
I don't know how to run Java in command prompt.
two jar files
E:\QR Code\lib\core-2.2.jar
E:\QR Code\lib\javase-2.2.jar
Java code path
E:\QR Code\src\com\javapapers\java\QRCode.java
Source code
http://javapapers.com/core-java/java-qr-code
Explain me how to run the java in command prompt
Regards
Karthikeyan K
To make sure that Windows can find the Java compiler and interpreter:
Select Start -> Computer -> System Properties -> Advanced system settings Environment Variables -> System variables -> PATH. ...
Prepend C:\Program Files\Java\jdk1.6.0_27\bin; to the beginning of the PATH variable.
Click OK
Then open command prompt and check if Java path setup done by below command
java -version
If everything ok it will show you which Java version you have installed .
then you can run java program Like below a example
C:\test> javac HelloWorld.java
I have test folder and it have HelloWorld.java file inside it after this
C:\test> java HelloWorld
It will give you output in my file i have added below content
public class HelloWorld{
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}

Java install - Doesn't run the classes

I'm getting started with java development. So, I installed JRE and JDK in my computer.
Then, I created a simple Example.java file and saved it in my Desktop.
In the prompt, I executed
javac Example.java
and it worked ok. A .class file has been created in my Desktop.
Then, I tried to run the class, executing this:
java Example
and I got an error in a window alert, with this message:
Java Installation Not Completed
Unable to install Java
There are error in the command line switches: "Example";.
Check that the commands are valid and try again
Then, for testing, I executed both commands:
javac -version and
java -version. Both are installed in my computer.
What am I doing wrong?
I am running Windows 8 and have already set my environment variables.
Example.java:
public class Example {
public static void main(String args[]) {
System.out.println("Finally Java");
}
}
Try to remove installation again, look for all leftovers and remove them manually, if you changed the directory you installed java to, remove environment variables as well and set them again. You should also make registry cleanup: https://java.com/en/download/help/manual_regedit.xml Make installation through offline installer.

Unable to run HelloWorld.java from command prompt

I got a strange problem in my windows 7 system running jdk1.6.0_33
When I try to run a simple java program from command prompt, it opens a new window (something like java frame) and suddenly disappears. There is no result shown on command prompt also I am unable to terminate the process (using Ctrl+C) or close command prompt after this. A java process is created each time I do this. I tried to kill process using Task Manager, but that too didn't work.
I am able to run the same program using eclipse.
Here is my program
class HelloWorld{
public static void main(String [] args)
{
System.out.println("Hello World");
}
}
Environment variables are set as follows:
Path=C:\Program Files\Java\jdk1.6.0_33\bin
classpath=.
Commands I used are,
javac HelloWorld.java
java HelloWorld
Why is this happening? Thanks in Advance.
I'm not sure, but I think this is what happens if you use java versus javaw from the command prompt on Windows:
References:
The java command manual page for Windows.
Difference between java/javaw/javaws
If your java class path is configured correctly.
type javac command in command prompt
You will get java compiler information.
After compile the class find your folder location if any .class file is created.

Categories