我的程序不会通过命令行编译,即使一切都在Eclipse中工作

时间:2018-02-07 15:07:48

标签: java compiler-errors

我无法弄清楚为什么在命令提示符下使用javac时我的程序无法编译。但是它在eclipse中工作并且没有错误出现。怎么可能在我的笔记本电脑上编译而没有任何Java版本1.8.0的问题,而不是在运行9.0.1的桌面计算机上

这是我在cmd上尝试编译时得到的内容

    test.java:14: error: error while writing Test:
    C:\Users\JohnD\Desktop\Test\Test.class
    public class Test {

    1 error

我的代码:

import java.util.*;

public class Test {

public static void main(String[] args) {
    BookCollection bc = new BookCollection ();
    bc.addNewBook("Big Java", "Horstmann", 2014, 150.0,"Paperback");
    bc.addUsedBook("Java", "Deitel", 1999, 120.0, 90.0);
    bc.addNewBook("JavaScript", "Hoque", 2005, 80.50, "Hardcover");
    bc.addNewBook("C++", "Smith", 2004, 135.0, "Paperback");
    bc.addUsedBook("C", "Jones", 2000, 110.0, 66.0);

    bc.printReport();
    System.out.println("****************************************");
    bc.printAllBooksWithSellingPriceBelow(100.0);
    bc.printAllPaperbackBooks();
 }
}
/////////////////////////////////////////////////////////////////////BOOK CLASS

class Book{
private String title;
private String author;
private int year;
private double price;
private String type; 
private double sellingPrice;

Book(String title, String author, int year, double price){}
                     /*****************************************************/

public String toString() {
    return "Title: " + title + "\nAuthor: " + author + "\nYear: " + year + "\nPrice: " + price ;

}
                   /*********************SET METHODS********************************************/   
void setTitle(String t) {
    title = t;
}
void setAuthor(String auth) {
    author = auth;
}
void setYear(int yr) {
    year = yr;
}
void setPrice(double prce) {
    price = prce;
}

void setType(String t) {
    type = t;
}

void setSellingPrice(double sp) {
    sellingPrice = sp;
}
                /***************************GET METHODS*******************************/ 
String getType() {
    return type;
}

double getPrice() {
    return price;
}

double getSellingPrice() {
    return sellingPrice;
} 
}

 ///////////////////////////////////////////////////////////////////NEW BOOK CLASS

class NewBook extends Book{
private String type;
NewBook(String NEWBOOK_TITLE, String NEWBOOK_AUTHOR, int NEWBOOK_YEAR, double NEWBOOK_PRICE, String NEWBOOK_TYPE){
    super(NEWBOOK_TITLE, NEWBOOK_AUTHOR, NEWBOOK_YEAR, NEWBOOK_PRICE);
        super.setTitle(NEWBOOK_TITLE);
        super.setAuthor(NEWBOOK_AUTHOR);
        super.setYear(NEWBOOK_YEAR);
        super.setPrice(NEWBOOK_PRICE);
        type =NEWBOOK_TYPE;
        super.setType(type);
}

public String toString() {
    return super.toString() + "\nType: " + type + "\n" ;
}
}

   //////////////////////////////////////////////////////////////////////USED BOOK CLASS

class UsedBook extends Book{
private double sellingPrice;
UsedBook(String USEDBOOK_TITLE, String USEDBOOK_AUTHOR, int USEDBOOK_YEAR, double USEDBOOK_PRICE, double USEDBOOK_SELLINGPRICE){

    super(USEDBOOK_TITLE, USEDBOOK_AUTHOR, USEDBOOK_YEAR, USEDBOOK_PRICE);
    super.setTitle(USEDBOOK_TITLE);
    super.setAuthor(USEDBOOK_AUTHOR);
    super.setYear(USEDBOOK_YEAR);
    super.setPrice(USEDBOOK_PRICE);
    sellingPrice = USEDBOOK_SELLINGPRICE;
    super.setSellingPrice(sellingPrice);
}

public String toString(){
    return super.toString() + "\nSelling Price: " + sellingPrice + "\n" ;
}

 }
 ////////////////////////////////////////////////////////////////////////Book Collection

class BookCollection{
private int MAX_LIMIT=1000;
private int count=0;
private ArrayList<Book> collection = new ArrayList <Book>(count);

void addNewBook(String pTitle, String pAuthor, int pYear, double pPrice, String pType) {
    if (count < MAX_LIMIT) {
        count++;
        NewBook nb = new NewBook(pTitle, pAuthor, pYear, pPrice, pType);
        collection.add(nb);
    }else {
        System.out.println("max volume");
    }   
}
                           /*****************************************************************/  
void addUsedBook(String pTitle, String pAuthor, int pYear, double pPrice, double pSellingPrice) {
    if (count < MAX_LIMIT) {
        count++;
        UsedBook ub = new UsedBook(pTitle, pAuthor, pYear, pPrice, pSellingPrice);
        collection.add(ub);
    }else {
        System.out.println("max volume");
    }
}
                            /*****************************************************************/

void printReport() {
    int bookIndex=0;
    for(int i = 0; i<collection.size(); i++){
        bookIndex++;
        if(collection.get(i).getType() != null) {
            System.out.println("Book " + bookIndex +":" + " New book" +"\n"+ collection.get(i));
        }else 
            System.out.println("Book " + bookIndex +":" +" Used book" +"\n"+ collection.get(i));
    }
}

                            /**********************************************************/    
void printAllBooksWithSellingPriceBelow(double budget) {
        int index=0;
    System.out.println(" \n******* BOOKS BELOW  $" + budget);
    for (int j = 0; j< collection.size(); j++) {
        index++;
        if (collection.get(j).getSellingPrice()!= 0.0){
            System.out.println("Book " + index +":Used Book" +" \n" + collection.get(j));
        }
        if (collection.get(j).getType() != null && collection.get(j).getPrice()<100) {
            System.out.println("Book " + index +":New Book" + " \n" + collection.get(j));
        }
    }
}
                        /*****************************************************************/

void printAllPaperbackBooks() {
    int index = 0; 
    System.out.println("*********" + " ALL PAPERBACK BOOKS");       
    for (int i = 0; i< collection.size();i++) {
        index++;
        String type = collection.get(i).getType();
        if(type != null && type.equals("Paperback")) {
            System.out.println("Book " + index +":NewBook" +"\n" + collection.get(i));
        }
    }
}
}

2 个答案:

答案 0 :(得分:0)

它为我编译你根本不会正确设置JDK。这不是一个答案,但似乎是一个环境问题。

显示输出

Book 1: New book
Title: Big Java
Author: Horstmann
Year: 2014
Price: 150.0
Type: Paperback

Book 2: Used book
Title: Java
Author: Deitel
Year: 1999
Price: 120.0
Selling Price: 90.0

Book 3: New book
Title: JavaScript
Author: Hoque
Year: 2005
Price: 80.5
Type: Hardcover

Book 4: New book
Title: C++
Author: Smith
Year: 2004
Price: 135.0
Type: Paperback

Book 5: Used book
Title: C
Author: Jones
Year: 2000
Price: 110.0
Selling Price: 66.0

****************************************

******* BOOKS BELOW  $100.0
Book 2:Used Book 
Title: Java
Author: Deitel
Year: 1999
Price: 120.0
Selling Price: 90.0

Book 3:New Book 
Title: JavaScript
Author: Hoque
Year: 2005
Price: 80.5
Type: Hardcover

Book 5:Used Book 
Title: C
Author: Jones
Year: 2000
Price: 110.0
Selling Price: 66.0

********* ALL PAPERBACK BOOKS
Book 1:NewBook
Title: Big Java
Author: Horstmann
Year: 2014
Price: 150.0
Type: Paperback

Book 4:NewBook
Title: C++
Author: Smith
Year: 2004
Price: 135.0
Type: Paperback

答案 1 :(得分:0)

我同意Prathibha Chiranthana

c:\TMP\Test>c:\java\jdk-9.0.4\bin\java.exe -version
java version "9.0.4"
Java(TM) SE Runtime Environment (build 9.0.4+11)
Java HotSpot(TM) 64-Bit Server VM (build 9.0.4+11, mixed mode)

c:\TMP\Test>c:\java\jdk-9.0.4\bin\javac.exe -version
javac 9.0.4

c:\TMP\Test>dir
 Volume in drive C is OSDisk
 Volume Serial Number is DA2F-C2CC

 Directory of c:\TMP\Test

2018-02-07  17:11    <DIR>          .
2018-02-07  17:11    <DIR>          ..
2018-02-07  17:07             5,771 Test.java
               1 File(s)          5,771 bytes
               2 Dir(s)  13,145,710,592 bytes free

c:\TMP\Test>c:\java\jdk-9.0.4\bin\javac.exe Test.java

c:\TMP\Test>

您是否尝试过不同的文件夹,而不是C:\ Users \ JohnD \ Desktop \?