【问题标题】:Using Java package from command line - cannot locate or load main从命令行使用 Java 包 - 无法找到或加载 main
【发布时间】: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


【解决方案1】:

请尝试这个快速解决方法。

创建文件夹层次结构 com\company 或 com/company(取决于您的操作系统)。

将 myclassname.class 文件放入 com\company 文件夹中。

从顶级文件夹(与com文件夹同级),运行

java com.company.myclassname

问候, 拉维

【讨论】:

  • 不走运。默认文件夹具有 com/company 层次结构,在我尝试使用包名称构建子文件夹之前,我已经尝试了这两个。我只是再次尝试,结果相同。
  • 希望它对我的教授有用。感谢您的回答!即使它对我不起作用,如果它对他有用,那才是最重要的。
猜你喜欢
  • 1970-01-01
  • 2014-08-02
  • 1970-01-01
  • 2014-07-04
  • 1970-01-01
  • 2016-01-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多