2017-08-08 86 views
0

我正在尋找一種方法來從命令行在同一個包下運行一個類的集羣,但是儘管成功編譯,我仍然得到「無法加載主「錯誤。我已經將路徑和類路徑更改爲他們需要的,以及嘗試構建一個名爲我使用的軟件包的子文件夾(「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 
    } 
} 

加一個輸入法

+0

你運行'java的融爲一體。來自'com'父目錄的company.myclassname'?您的IDE應該可能會打印使用的java命令,您可以將其用作參考。另外,確保你的'com.company.myclassname'類有一個'public static void main(String [] args)'方法。 – Beginner

+0

發佈'myclassname.java'的源代碼。最重要的是,向我們展示該文件中的軟件包名稱,並告訴我們您用於存儲該Java文件的位置(哪個目錄結構)。很可能,你只是有一個小錯誤。 –

+0

我一直在上下目錄樹。父文件夾沒有區別。 – CodeBlue04

回答

-1

請嘗試這種快速的解決方法。

創建一個文件夾層次COM \公司或COM /公司(取決於您的操作系統)。

把myclassname.class文件中的COM \公司文件夾內。

從頂層文件夾(這是在同一級別的COM文件夾),運行

的Java com.company.myclassname

問候, 拉維

+0

沒有運氣。默認文件夾具有com/company層次結構,並且在我嘗試用軟件包名稱構建子文件夾之前,我試過了這兩個文件夾。我剛剛嘗試了相同的結果。 – CodeBlue04

+0

它適合我。 –

+0

希望它能爲我的教授工作。感謝你的回答!即使它對我不起作用,如果它對他有效,那也是最重要的。 – CodeBlue04

相關問題