[英]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
类和类common
包QuickSortTests
。 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.