简体   繁体   中英

Using Java package from command line - cannot locate or load main

I'm looking for a way to run a cluster of classes under the same package from the command line, but despite successfully compiling, I keep getting "could not load main" errors. I've changed the path and classpath to what they need to be, as well as tried building a subfolder named for the package I'm using ("com.company"), but to no avail. I've tried the following on command line while in the package-named subfolder directory, as well as the folder above that:

>java com.company.myclassname
>java myclassname
>java com\company\myclassname
>java -cp . com.company.myclassname

All have left me with the same "Error: Could not find or load main class".

At this point I've been poring over StackOverflow questions and tutorials for 3 hours to avoid having a repeat question, but I'm desperate. I've got to turn this homework assignment in in two hours. It works just fine within my IDE, and even through my backup beta IDE, but not command line. Can anyone please shed some light on this for me?

Edit: Source code:

package com.company;

import static com.company.myclassname.quantInput;
import static com.company.myclassname.costInput;

public class GroceryList {//This is the parent class for the program.
private static int counter = 0;//Used to ensure that the number of items is limited to ten.
private static GroceryList[] List = new GroceryList[10];//Used to hold the first ten grocery items in the add method.


public GroceryList(){//Basic constructor
}

.... Plus a few methods.

Client code:

package com.company;

import java.io.File;
import java.io.FileNotFoundException;
import java.text.DecimalFormat;
import java.util.Scanner;

public class myclassname {

private static String[] nameInput = new String[10];//for holding names from each line, then gets sent to constructor by index
public static int[] quantInput = new int[10];//for holding quantity from each line, then gets sent to constructor by index
public static double[] costInput = new double[10];//for holding price from each line, then gets sent to constructor by index
public static GroceryItemOrder[] GIOList = new GroceryItemOrder[10];//for holding GroceryItemOrder objects, then gets sent to toString() for printing
public static double TotalCost = 0;//initializes total cost variable
public static DecimalFormat f = new DecimalFormat("#####0.00");//Ensures proper output format for doubles
private static int counter;//Used for indexing
private static String target;//File path

public static void main(String[] args) throws FileNotFoundException, NullPointerException {
    target = args[0];//User-supplied file path is assigned to variable "target"
    try {//protects against NullPointerException
        input();//Sends file path to input method, which sends that data to all other relevant methods and classes
        System.out.printf("%-20s", "Item");//These lines provide headers for output message
        System.out.printf("%-10s", "Quantity");
        System.out.printf("%-10s", "Price");
        System.out.printf("%-12s", "Total Price");
        System.out.println();
        for (int i = 0; i < counter; i++) {//Ensures only correct objects are printed to user
            System.out.println(GIOList[i].toString());//public object array sends data to the toString() method, which
            //then prints formatted output string, limited by counter in order to ensure only proper data prints
        }if (counter<10){//If the file contains under 11 items, this prints to the user
        System.out.println("Total cost: $" + f.format(TotalCost));}
        else{//if the file contains 11 or more lines, this statement makes clear that only the first 10 items
            //will be included in the total cost.
            System.out.println("Total cost of the first ten items in your file: $" + f.format(TotalCost));
        }
    } catch (NullPointerException e){//safeguard against printing null strings to user
    }
}

Plus an input method

Please try this quick workaround.

create a folder hierarchy com\\company or com/company (depending on you OS).

Put the myclassname.class file inside the com\\company folder.

from top level folder (which is at same level as com folder), run

java com.company.myclassname

Regards, Ravi

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM