我正在尋找一種方法來從命令行在同一個包下運行一個類的集羣,但是儘管成功編譯,我仍然得到「無法加載主「錯誤。我已經將路徑和類路徑更改爲他們需要的,以及嘗試構建一個名爲我使用的軟件包的子文件夾(「com.company」),但無濟於事。我試着在命令行下,而在包命名的子文件夾的目錄,以及文件夾上面說:從命令行使用Java包 - 無法找到或加載主
>java com.company.myclassname
>java myclassname
>java com\company\myclassname
>java -cp . com.company.myclassname
所有已經給我留下了相同的「錯誤:無法找到或加載主類」。
在這一點上,我一直在StackOverflow的問題和教程3小時俯視,以避免有一個重複的問題,但我絕望。我必須在兩個小時內完成這項家庭作業。它在我的IDE中工作得很好,甚至通過我的備份測試版IDE,但不是命令行。任何人都可以爲我澄清這一點嗎?
編輯:源代碼:
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
}
....再加上一些方法。
客戶端代碼:
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
}
}
加一個輸入法
你運行'java的融爲一體。來自'com'父目錄的company.myclassname'?您的IDE應該可能會打印使用的java命令,您可以將其用作參考。另外,確保你的'com.company.myclassname'類有一個'public static void main(String [] args)'方法。 – Beginner
發佈'myclassname.java'的源代碼。最重要的是,向我們展示該文件中的軟件包名稱,並告訴我們您用於存儲該Java文件的位置(哪個目錄結構)。很可能,你只是有一個小錯誤。 –
我一直在上下目錄樹。父文件夾沒有區別。 – CodeBlue04