我有2个java项目,其中一个是java web。我正在尝试从我的PoliceWeb应用程序编译和运行java应用程序PoliceCars。 当通过netbeans运行时,PoliceCars可以自行编译并运行。 但是,当通过PoliceWeb编译时,我收到了50多个编译错误。 我会发布代码,然后发布我的问题。 CompileAndRun.java - 在PoliceWeb内:
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.file.Paths;
public class CompileAndRun {
private static String cPath = "/home/noa/NetBeansProjects/PC/src/pc/PoliceCars.java";
private static String runPath = "/home/noa/NetBeansProjects/PC/src/pc/PoliceCars";
/**
*
* @param args
*/
public static void main(String[] args) throws Exception {
new CompileAndRun(args[0], args[1], args[2]);
}
public CompileAndRun(String path, String date, String time) throws Exception {
try {
comAndRun(" "+ path + " "+ date + " " + time);
} catch (IOException | InterruptedException ex) {
ex.printStackTrace();
}
}
public int comAndRun(String param) throws Exception {
//compile PoliceCars.java
Process p = Runtime.getRuntime().exec("javac -cp pc "+cPath);
// I've tried also:("javac "+cPath) and different variations
printLines(" P javac stdout:", p.getInputStream());
//print the compilation errors
printLines(" P javac stderr:", p.getErrorStream());
int result = p.waitFor();
//run PoliceCars
Process p2 = Runtime.getRuntime().exec("chmod u+x "+runPath + param);
printLines(" stdout:", p2.getInputStream());
printLines(" stderr:", p2.getErrorStream());
result = p2.waitFor();
System.out.println(" exitValue() " + p2.exitValue());
System.out.println("running result = " +result);
return result;
}
private static void printLines(String cmd, InputStream ins) throws Exception {
String line = null;
BufferedReader in = new BufferedReader(
new InputStreamReader(ins));
while ((line = in.readLine()) != null) {
System.out.println(cmd + " " + line);
}
}
}
PoliceCars.java:
package pc;
import com.google.common.collect.HashBiMap;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import static java.lang.Math.abs;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jgrapht.graph.DefaultEdge;
public class PoliceCars {
private static List<List<ConcreteData>> weatherList;
private static List<List<ConcreteData>> roadList;
private static List<List<ConcreteData>> accidentsList;
private static List<List<ConcreteData>> countList;
private static List<List<ConcreteData>> placementList;
private static List<ConcreteData> placedCars;
private static List<List<RoadData>> roadsPlacedOn;
private static RoadGraph rg;
private static pathsForEveryPoliceCar dpc;
private static String userFile;
private static String userDate;
private static String userTime;
/**
* @param args the command line arguments
* @throws java.io.FileNotFoundException
*/
public static void main(String[] args) throws FileNotFoundException, IOException{
List<String> countFileNames = new LinkedList<>();
List<String> weatherFileNames = new LinkedList<>();
List<String> roadFileNames = new LinkedList<>();
List<String> accidentsFileNames = new LinkedList<>();
List<String> placementFileNames = new LinkedList<>();
List<RoadData> roads = new LinkedList<>();
List<String> lines = Arrays.asList("The first line", "The second line");
Path p = Paths.get("f1.txt");
Files.write(p, lines, Charset.forName("UTF-8"));
weatherList = new LinkedList<>();
roadList = new LinkedList<>();
accidentsList = new LinkedList<>();
countList = new LinkedList<>();
placementList = new LinkedList<>();
placedCars = new LinkedList<>();
roadsPlacedOn = new LinkedList<>();
List<RoadData> allTheRoadsSegments = new LinkedList<>();
String filePath = RunPy.run("/home/noa/Documents/untitled/HelloWorld.py", "27.01.2015", "08:00");
lines = Arrays.asList("The first line", "The second line");
p = Paths.get("the-file-name.txt");
Files.write(p, lines, Charset.forName("UTF-8"));
System.out.println("");
}
/**
* this function is for running the program all day long
*/
private static void programLoop(List<RoadData> allTheRoadsSegments) throws IOException{
HourlyStats hs = new HourlyStats(rg, allTheRoadsSegments);
dpc = new pathsForEveryPoliceCar(rg, allTheRoadsSegments);
Map <Integer, String> statsForEveryHour = new HashMap<>();
List<String> placementFileNames = new LinkedList<>();
List<List<ConcreteData>> placementList1 = new LinkedList<>();
List<ConcreteData> oldPlacementList = new LinkedList<>();
placementFileNames.add("data/police_cars_places.csv");
while(true){
try {
LocalDateTime clock = LocalDateTime.now();
//getting the police cars placement for this hour
Parser pp = new PoliceCarsCSVParser();
placementList1 = pp.parseFromFiles(placementFileNames);
for(List<ConcreteData> pl : placementList1){
for(ConcreteData pCar : pl){
if(((PoliceCarsPlacement)pCar).getPoliceCarPlaced().equals("1") &&
((PoliceCarsPlacement)pCar).getHour().equals(String.valueOf(clock.getHour())))
placedCars.add(pCar);
if(((PoliceCarsPlacement)pCar).getPoliceCarPlaced().equals("1") &&
((PoliceCarsPlacement)pCar).getHour().equals(String.valueOf(clock.getHour()-1))){
oldPlacementList.add(pCar);
}
}
}
setroadsPlacesOn();
//publish the police car placement
OutputPoliceCars opc = new OutputPoliceCars(roadList, 15 ,
clock, 9, 19, roadsPlacedOn);
SaveToFile sv1 = new SaveToFile("police_cars_places.csv", opc.writeToFile());
dpc.setPlaces(oldPlacementList, placedCars);
statsForEveryHour.put(clock.getHour(), hs.getDistancesBEtweenCars(placedCars));
oldPlacementList = placedCars;
placedCars.clear();
if (clock.getHour() == 19)
break;
Thread.sleep(45*60*1000);
} catch (InterruptedException ex) {
Logger.getLogger(PoliceCars.class.getName()).log(Level.SEVERE, null, ex);
}
}
File file = new File("outputData/Hourly_stats.txt");
FileWriter writer = new FileWriter (file, true);
PrintWriter out = new PrintWriter(writer);
for (Map.Entry<Integer, String> entry : statsForEveryHour.entrySet()){
out.print(String.valueOf(entry.getKey()) + ' ');
out.println(entry.getValue());
}
DistancesforEveryPoliceCar d = new DistancesforEveryPoliceCar(dpc.getPaths(), rg);
}
private static void setroadsPlacesOn(){
int k = 0, i = 0;
List<RoadData> roads = new LinkedList<>();
for(List<ConcreteData> rl : roadList){
k = 0;
i += 1;
System.out.println("i = " + i);
if(roads.size() > 0){
roads = new LinkedList<>();
}
for(ConcreteData pCar : placedCars){
for(ConcreteData r : rl){
if(k > 0 && (((PoliceCarsPlacement)pCar).getRoadNum()).equals(((RoadData)r).getRoadNumber()) &&
(((PoliceCarsPlacement)pCar).getRoadSegement()).equals((((RoadData)r).getRoadSegment()) )){
roads.add((RoadData) r);
}
k += 1;
}
roadsPlacedOn.add(roads);
}
}
}
public static String getDataOfDate(String date, String time) {
String str = "";
for (List<ConcreteData> list : weatherList) {
for (ConcreteData concreteData : list) {
if (((WeatherData) concreteData).getDate().equals(date)) {
if (((WeatherData) concreteData).GetTime().equals(time)) {
str = "weather info for given date:\n" + concreteData.GetData();
break;
}
}
}
}
//concat to str rest of data after parsed.
return str;
}
/**
*
* @param w - sets the weather list according to the user decision
* Static or dynamic
*/
public void setWeatherList (List<List<ConcreteData>> w){
weatherList = w;
}
/**
*
* @param rd - RoadData list - to save all the roads segments
*/
protected static void createGraph(List<RoadData> rd){
rg = new RoadGraph();
int i,j;
for (i = 0; i < roadList.get(0).size(); i++)
rg.AddVertex((RoadData) roadList.get(0).get(i));
for (i = 0; i < roadList.get(0).size(); i++){
RoadData r1 = (RoadData) roadList.get(0).get(i);
rd.add(r1);
for (j = 0; j < roadList.get(0).size(); j++){
if (i != j){
RoadData r2 = (RoadData) roadList.get(0).get(j);
if (r1.getFrom().equals(r2.getFrom()) || r1.getTo().equals(r2.getFrom())
|| r1.getFrom().equals(r2.getTo()) || r1.getTo().equals(r2.getTo()))
rg.AddeEdge(r1, r2);
}
}
}
}
/**
* returns the time to go between roads in minutes (it goes between the middle
* of the source to the middle of the target segment
* @param sourceStart - the km number of the beginning of the segment of the source segment
* @param sourceEnd - the km number of the end of the segment of the source segment
* @param targetStart - the km number of the beginning of the segment of the target segment
* @param TargetEnd - the km number of the end of the segment of the target segment
* @param sourceSpeed - the source segment speed limit
* @param targetSpeed - the target segment speed limit
* @return - the amount of time to drive from the source segment to the target segment
*/
protected String returnMin(String sourceStart, String sourceEnd, String targetStart, String TargetEnd
,String sourceSpeed, String targetSpeed){
float diffSource = (abs(Float.parseFloat(sourceStart) - Float.parseFloat(sourceEnd))/2),
diffTarget = (abs(Float.parseFloat(targetStart) - Float.parseFloat(TargetEnd))/2);
float source = Float.parseFloat(sourceSpeed), target = Float.parseFloat(targetSpeed);
float time = (diffSource/source) + (diffTarget/target);
int a = (int) Math.round(time*60);
return Integer.toString(a);
}
}
在包pc中我有所有必要的类。 从PoliceWeb编译PoliceCars.java时,我收到以下错误:
Info: P javac stderr: /home/noa/NetBeansProjects/PC/src/pc/PoliceCars.java:39: error: cannot find symbol
Info: P javac stderr: private static List<List<ConcreteData>> weatherList;
Info: P javac stderr: ^
Info: P javac stderr: symbol: class ConcreteData
Info: P javac stderr: location: class PoliceCars
Info: P javac stderr: /home/noa/NetBeansProjects/PC/src/pc/PoliceCars.java:40: error: cannot find symbol
Info: P javac stderr: private static List<List<ConcreteData>> roadList;
Info: P javac stderr: ^
Info: P javac stderr: symbol: class ConcreteData
Info: P javac stderr: location: class PoliceCars
Info: P javac stderr: /home/noa/NetBeansProjects/PC/src/pc/PoliceCars.java:41: error: cannot find symbol
Info: P javac stderr: private static List<List<ConcreteData>> accidentsList;
Info: P javac stderr: ^
Info: P javac stderr: symbol: class ConcreteData
Info: P javac stderr: location: class PoliceCars
Info: P javac stderr: /home/noa/NetBeansProjects/PC/src/pc/PoliceCars.java:42: error: cannot find symbol
Info: P javac stderr: private static List<List<ConcreteData>> countList;
Info: P javac stderr: ^
Info: P javac stderr: symbol: class ConcreteData
Info: P javac stderr: location: class PoliceCars
Info: P javac stderr: /home/noa/NetBeansProjects/PC/src/pc/PoliceCars.java:43: error: cannot find symbol
Info: P javac stderr: private static List<List<ConcreteData>> placementList;
Info: P javac stderr: ^
Info: P javac stderr: symbol: class ConcreteData
Info: P javac stderr: location: class PoliceCars
Info: P javac stderr: /home/noa/NetBeansProjects/PC/src/pc/PoliceCars.java:44: error: cannot find symbol
Info: P javac stderr: private static List<ConcreteData> placedCars;
Info: P javac stderr: ^
Info: P javac stderr: symbol: class ConcreteData
Info: P javac stderr: location: class PoliceCars
Info: P javac stderr: /home/noa/NetBeansProjects/PC/src/pc/PoliceCars.java:45: error: cannot find symbol
Info: P javac stderr: private static List<List<RoadData>> roadsPlacedOn;
Info: P javac stderr: ^
Info: P javac stderr: symbol: class RoadData
Info: P javac stderr: location: class PoliceCars
Info: P javac stderr: /home/noa/NetBeansProjects/PC/src/pc/PoliceCars.java:46: error: cannot find symbol
Info: P javac stderr: private static RoadGraph rg;
Info: P javac stderr: ^
Info: P javac stderr: symbol: class RoadGraph
Info: P javac stderr: location: class PoliceCars
Info: P javac stderr: /home/noa/NetBeansProjects/PC/src/pc/PoliceCars.java:47: error: cannot find symbol
Info: P javac stderr: private static pathsForEveryPoliceCar dpc;
Info: P javac stderr: ^
Info: P javac stderr: symbol: class pathsForEveryPoliceCar
Info: P javac stderr: location: class PoliceCars
Info: P javac stderr: /home/noa/NetBeansProjects/PC/src/pc/PoliceCars.java:92: error: cannot find symbol
Info: P javac stderr: private static void programLoop(List<RoadData> allTheRoadsSegments) throws IOException{
Info: P javac stderr: ^
Info: P javac stderr: symbol: class RoadData
Info: P javac stderr: location: class PoliceCars
Info: P javac stderr: /home/noa/NetBeansProjects/PC/src/pc/PoliceCars.java:215: error: cannot find symbol
Info: P javac stderr: public void setWeatherList (List<List<ConcreteData>> w){
Info: P javac stderr: ^
Info: P javac stderr: symbol: class ConcreteData
Info: P javac stderr: location: class PoliceCars
Info: P javac stderr: /home/noa/NetBeansProjects/PC/src/pc/PoliceCars.java:223: error: cannot find symbol
Info: P javac stderr: protected static void createGraph(List<RoadData> rd){
Info: P javac stderr: ^
Info: P javac stderr: symbol: class RoadData
Info: P javac stderr: location: class PoliceCars
Info: P javac stderr: /home/noa/NetBeansProjects/PC/src/pc/PoliceCars.java:65: error: cannot find symbol
Info: P javac stderr: List<RoadData> roads = new LinkedList<>();
Info: P javac stderr: ^
Info: P javac stderr: symbol: class RoadData
Info: P javac stderr: location: class PoliceCars
Info: P javac stderr: /home/noa/NetBeansProjects/PC/src/pc/PoliceCars.java:79: error: cannot find symbol
Info: P javac stderr: List<RoadData> allTheRoadsSegments = new LinkedList<>();
Info: P javac stderr: ^
Info: P javac stderr: symbol: class RoadData
Info: P javac stderr: location: class PoliceCars
Info: P javac stderr: /home/noa/NetBeansProjects/PC/src/pc/PoliceCars.java:80: error: cannot find symbol
Info: P javac stderr: String filePath = RunPy.run("/home/noa/Documents/untitled/HelloWorld.py", "27.01.2015", "08:00");
Info: P javac stderr: ^
Info: P javac stderr: symbol: variable RunPy
Info: P javac stderr: location: class PoliceCars
Info: P javac stderr: /home/noa/NetBeansProjects/PC/src/pc/PoliceCars.java:94: error: cannot find symbol
Info: P javac stderr: HourlyStats hs = new HourlyStats(rg, allTheRoadsSegments);
Info: P javac stderr: ^
Info: P javac stderr: symbol: class HourlyStats
Info: P javac stderr: location: class PoliceCars
Info: P javac stderr: /home/noa/NetBeansProjects/PC/src/pc/PoliceCars.java:94: error: cannot find symbol
Info: P javac stderr: RoadData r2 = (RoadData) roadList.get(0).get(j);
Info: P javac stderr: ^
Info: P javac stderr: symbol: class RoadData
Info: P javac stderr: location: class PoliceCars
Info: P javac stderr: /home/noa/NetBeansProjects/PC/src/pc/PoliceCars.java:234: error: cannot find symbol
Info: P javac stderr: RoadData r2 = (RoadData) roadList.get(0).get(j);
Info: P javac stderr: ^
Info: P javac stderr: symbol: class RoadData
Info: P javac stderr: location: class PoliceCars
Info: P javac stderr: 55 errors
这些是55个错误的一部分。我试过导入pc。*;但它没有帮助。 关于为什么我会得到这些错误的任何想法? 如何成功编译和运行PoliceCars? 有没有办法独立于操作系统进行编译?