2017-08-08 34 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

相关问题