【发布时间】:2018-01-15 12:11:42
【问题描述】:
我正在寻找一种方法来从命令行在同一个包下运行一组类,但尽管编译成功,但我不断收到“无法加载主程序”错误。我已将路径和类路径更改为所需的内容,并尝试构建一个以我正在使用的包命名的子文件夹(“com.company”),但无济于事。我在以包命名的子文件夹目录以及上面的文件夹中尝试了以下命令行:
>java com.company.myclassname
>java myclassname
>java com\company\myclassname
>java -cp . com.company.myclassname
所有人都给我留下了相同的“错误:无法找到或加载主类”。
在这一点上,我已经研究了 3 个小时的 StackOverflow 问题和教程,以避免出现重复的问题,但我很绝望。我必须在两个小时内交上这份家庭作业。它在我的 IDE 中运行良好,甚至通过我的备份 beta 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
}
}
加上一个输入法
【问题讨论】:
-
您是从
com的父目录运行java com.company.myclassname吗?您的 IDE 可能应该打印使用的 java 命令,您可以将其用作参考。此外,请确保您的com.company.myclassname类具有public static void main(String[] args)方法。 -
贴出
myclassname.java的源代码。最重要的是,向我们展示该文件中的包名称,并告诉我们您使用哪个目录结构来存储该 Java 文件。很可能,您只是犯了一个小错误。 -
我一直在目录树上下。父文件夹没有区别。
-
你真的先编译java代码吗?
-
好的。所以你的 myclassname.class 路径是 - C:\Users\myname\Downloads\myclassname\production\myclassname\com\company\myclassname.class 正确吗?现在,您可以从命令行尝试这个 - cd C:\Users\myname\Downloads\myclassname\production\myclassname 然后 java com.company.myclassname 让我知道它是否有效?
标签: java command-line packages