简体   繁体   English

javac throw软件包不存在,但是intellij可以编译并运行

[英]javac throw package doesn't exist but intellij can compile and run

I have a following directory structure: 我有以下目录结构:

-com
  -laicode
    -class1
      -QuickSort.java
    -common
      -someclasses
  -test
    -class1
      -QuickSortTests.java

I want to import QuickSort class and classes in common package in QuickSortTests . 我想导入QuickSort类和类commonQuickSortTests So the code in QuickSort.java looks like: 因此, QuickSort.java的代码如下所示:

package com.test.class1;

import java.util.Arrays;
import com.laicode.common.*;
import com.laicode.class1.QuickSort;

class QuickSortTests {
    public static void main(String[] args) {
        int[] array0 = null;
        QuickSort.quickSort(array0);
        System.out.println(Arrays.toString(array0));

        int[] array1 = new int[0];
        QuickSort.quickSort(array1);
        ...

When I complie QuickSortTests.java in cmd using javac QuickSortTests.java , it throws an error saying: 当我使用javac QuickSortTests.java QuickSortTests.java在cmd中javac QuickSortTests.java ,它抛出一条错误消息:

QuickSortTests.java:4: error: package laicode.common does not exist
import laicode.common.*;
QuickSortTests.java:5: error: package laicode.class1 does not exist
import laicode.class1.QuickSort;

But in Intellij, QuickSortTests can run without any errors. 但在的IntelliJ, QuickSortTests可以没有任何错误运行。

IntelliJ already knows your source layout, and even what classes are defined in what files. IntelliJ已经知道您的源布局,甚至知道哪些文件中定义了哪些类。 javac doesn't. javac没有。 There are multiple ways you could approach the problem, depending in part on where you want the compiled class files to go, but the simplest approach would probably be to make the root of your source tree your working directory, and run javac from there: 有多种方法可以解决此问题,部分取决于您希望将已编译的类文件放入何处,但是最简单的方法可能是将源树的根作为工作目录,然后从那里运行javac

javac com/test/class1/QuickSortTests.java

(Or use backslashes instead of forward slashes on Windows.) (或者在Windows上使用反斜杠而不是正斜杠。)

Note capitalization: I have assumed that the mismatch between the capitalization of your class names and that of the names of the files in which they reside is an error in your question . 注意大写:我假设您的类名和它们所在文件名的大写不匹配是您的问题 If these actually do disagree in your sources then you should fix the discrepancy. 如果这些在您的来源中确实不同意,那么您应该纠正差异。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法使用 javac 和 lombok 在控制台中编译.h 文件。 错误:package 龙目岛不存在 - Can't compile .h file in console with javac and lombok. Error: package lombok does not exist Java或JavaC继续说找不到或不存在说软件包 - Java or JavaC Keeps Saying Package Not Found or Doesn't Exist 无法在 IntelliJ 中编译/运行 java 项目 - Can't compile/run a java project in IntelliJ 为什么Eclipse编译这个,但javac不编译? - Why does Eclipse compile this, but javac doesn't? 尝试使用 javac 编译 java 源代码:package R 不存在 - Trying to compile java source with javac: package R does not exist 为什么 IntelliJ 会给我“包不存在”错误? - Why does IntelliJ give me "Package doesn't exist" error? Maven:编译项目时无法解析包不存在 - Maven: Cannot resolve package doesn't exist when compile project Intellij 可以构建我的项目,但 javac 无法编译/查找符号 - Intellij can build my project but javac cannot compile/find symbol javac(compile)和java(run)包在不同的目录结构级别上工作? - javac(compile) and java(run) package work at different directory structure levels? 为什么javac无法编译Java 1.5代码以在Java 1.4 JVM上运行? - Why can't javac compile Java 1.5 code to run on Java 1.4 JVMs?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM