所以,我的包结构是我有一个带有我的代码的src文件夹,里面有一个带有类和三个方法的exec文件夹,我的主类和两个罐子。
我的exec类看起来像这样:
package com.xxx.exec;
import au.com.bytecode.opencsv.CSVReader;
import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
/**
* Created by xxx on 24/07/16.
*/
public class ApiAccess {
public void getConsoleInput() throws Exception {
Console console = System.console();
if (console == null) {
throw new Exception("Unable to fetch console");
}
System.out.println("Please enter the location:");
getAPIData(console.readLine());
}
private void writeCsv(InputStream input) {
try {
CSVReader locationData = new CSVReader(new InputStreamReader(input));
locationData.close();
System.out.println(input);
}
catch (Exception e) {
System.out.println("Something went wrong while creating the csv:" + e);
}
}
private void getAPIData(String location) throws IOException {
String url = "http://exec.goeuro.com/exec/v2/position/suggest/en/";
String charset = StandardCharsets.UTF_8.name();
String query = String.format(url + "%s", URLEncoder.encode(location, charset));
URLConnection connection = new URL(query).openConnection();
connection.setRequestProperty("Accept-Charset", charset);
// String line;
// StringBuilder text = new StringBuilder();
// BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
// while((line = reader.readLine()) != null) {
// text.append(line).append(" ");
// }
//
// writeCsv(text.toString());
writeCsv(connection.getInputStream());
}
}
我的终端命令如下所示:
javac -cp ".:lib/*:opencsv-2.41.jar" -d src $(find ./src/* | grep .java)
我得到了这个错误:
./src/com/goeuro/exec/ApiAccess.java:3: error: package au.com.bytecode.opencsv does not exist
import au.com.bytecode.opencsv.CSVReader;
^
./src/com/goeuro/exec/ApiAccess.java:29: error: cannot find symbol
CSVReader locationData = new CSVReader(new InputStreamReader(input));
^
symbol: class CSVReader
location: class ApiAccess
./src/com/goeuro/exec/ApiAccess.java:29: error: cannot find symbol
CSVReader locationData = new CSVReader(new InputStreamReader(input));
^
symbol: class CSVReader
location: class ApiAccess
3 errors
提前感谢您的任何答案!
当前dir结构(我在目录前放了一个反斜杠):
/src >
-/com.xxx >
-Main.java,
-/exec >
-ApiAccess,
-opencsv-2.41.jar >
-/au.com.bytecode.opencsv,
-commons-lang3-3.0.1.jar
答案 0 :(得分:0)
我仍然不相信我理解你的目录结构。您的ASCII和图片似乎在说>>来源<<<<<<<<<<<<<<<<<目录,这是(IMO)有点疯狂。
相反,我推荐以下结构:
src/
com/
xxx/
Main.java // class name "com.xxx.Main"
exec/
APIAccess.java // class name "com.xxx.exec.APIAccess"
lib/
opencsv-2.41.jar
commons-lang3-3.0.1.jar
classes/
com/
xxx/
Main.class
exec/
APIAccess.class
并编译这样的类:
javac -cp "classes:lib/*" -d classes $(find src/* | grep .java)
像这样的应用程序运行:
java -cp "classes:lib/*" com.xxx.Main
关于上述内容的突出点:
(如果你没有使用版本控制,你应该学习如何做到这一点。它会为你节省很多伤害。另外,看看切换到Java构建工具。看看像Maven和Gradle这样可以处理的工具外部依赖很好。)