3

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.

smj7v3
  • 45
  • 7

3 Answers3

0

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

Kishore
  • 403
  • 4
  • 11
0

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

M J
  • 11
  • 1
-1

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
rsh
  • 69
  • 4
  • Just tested on my local machine.. try with below command after setting classpath -> java com.smj.programmingByDoing.FirstProg – rsh Jan 05 '17 at 20:51
  • Pardon my ignorance, but maybe I'm not understanding setting the classpath. Here is the location of the class: C:\Users\smj7v\workspace\LearningJava\src\com\smj\programmingByDoing Am I supposed to run "set classpath=C:\Users\smj7v\workspace\LearningJava\src\com\smj\programmingByDoing;.;" before executing? – smj7v3 Jan 05 '17 at 21:00
  • Thank you it works. I was taking it a step too far. I kept trying to execute from the directory: "C:\Users\smj7v\workspace\LearningJava\src\com\smj\‌​programmingByDoing" rather than from the src folder. Much appreciated for the help! – smj7v3 Jan 05 '17 at 21:38