-1

I compiled my java program using "javac LabOne.java" and it complied suscessfully. Now in that folder there is a LabOne.java and LabOne.class as expected. But when i try to run the program by using "java LabOne" i get an error saying "Error: Could not find or load main class LabOne".

Please can someone assist me.

Code :

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package labone;

import java.util.Scanner;

/**
 *
 *
 */
public class LabOne {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        Scanner userInput = new Scanner(System.in);

        System.out.println("Welcome To The String Editor!"); 
        System.out.println(""); 
        System.out.println("Please choose what you would like to do by choosing one of the options below:"); 
        System.out.println("1. Input String");
        System.out.println("2. Print Current String");
        System.out.println(""); 
        int userOption = userInput.nextInt();
        String stringInput = new String ();

        switch (userOption) {
            case 1: stringInput = userInput.nextLine();
                    System.out.println(stringInput);
                    break;

            case 2: System.out.println(stringInput);
                    break;

            default: ;
                     break;
        }


        // TODO code application logic here
    }

}
45aken
  • 23
  • 1
  • 2
  • 6

3 Answers3

1

The problem is because your class LabOne resides in a package called labone. You are most probably inside the folder labone and you ran javac LabOne.java and it succeeded. But when you will try to run it using java LabOne it is going to fail.

The solution is cd up one level and execute:

javac labone/LabOne.java 

java labone/LabOne

and it will work.

This answer deals with a similar problem.

You can also try adding the package to the classpath but it doen't look like your class is dependent on other classes for now, so doing that might be an overkill.

Community
  • 1
  • 1
SJha
  • 1,510
  • 1
  • 10
  • 17
0

The error says that the system cannot find the main method. There can be multiple reasons

  1. Is the class labOne 'public'?
  2. is the main method defined in the class 'labOne' and is defined public and static?
  3. Is the class name same as the file name and is case sensitive so should be exact same?

If both the above is not the case then please also share the code to be able to answer that.

Parth Satra
  • 513
  • 2
  • 16
0

I'd start with sorting out file/class naming issues, accessibility operators and then eventually take a look at CLASSPATH. You can play with that path both in system environment settings and using -cp option while invoking java compiler. I strongly encourage you to share more details.

Wojciech Fornal
  • 1,265
  • 11
  • 11