[英]How to properly compile this file using javac
我有一個名為Test.Java的以下文件,其代碼為
package example25;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.Iterator;
import org.jibx.runtime.BindingDirectory;
import org.jibx.runtime.IBindingFactory;
import org.jibx.runtime.IMarshallingContext;
import org.jibx.runtime.IUnmarshallingContext;
import org.jibx.runtime.JiBXException;
public class Test
{
public static void main(String[] args)
{
try
{
// unmarshal customer information from file
IBindingFactory bfact = BindingDirectory.getFactory(Order.class);
IUnmarshallingContext uctx = bfact.createUnmarshallingContext();
FileInputStream in = new FileInputStream("D:\\Java Libraries\\jibx\\dwcode2\\starter.xml");
Order order = (Order)uctx.unmarshalDocument(in, null);
// compute the total amount of the order
float total = 0.0f;
for (Iterator<Item> iter = order.getItemList().iterator(); iter.hasNext();)
{
Item item = iter.next();
total += item.getPrice() * item.getQuantity();
}
order.setTotal(new Float(total));
// marshal object back out to file (with nice indentation, as UTF-8)
IMarshallingContext mctx = bfact.createMarshallingContext();
mctx.setIndent(2);
FileOutputStream out = new FileOutputStream("c:\\out.xml");
mctx.setOutput(out, null);
mctx.marshalDocument(order);
System.out.println("Processed order with " + order.getItemList().size() + " items and total value " + total);
}
catch (FileNotFoundException e)
{
e.printStackTrace();
System.exit(1);
} catch (JiBXException e)
{
e.printStackTrace();
System.exit(1);
}
}//end main
}//end class
這是我嘗試編譯此文件以及我得到的輸出的方式
C:\jibx\tutorial>javac example25\Test.java
example25\Test.java:8: error: package org.jibx.runtime does not exist
import org.jibx.runtime.BindingDirectory;
^
example25\Test.java:9: error: package org.jibx.runtime does not exist
import org.jibx.runtime.IBindingFactory;
^
example25\Test.java:10: error: package org.jibx.runtime does not exist
import org.jibx.runtime.IMarshallingContext;
^
example25\Test.java:11: error: package org.jibx.runtime does not exist
import org.jibx.runtime.IUnmarshallingContext;
^
example25\Test.java:12: error: package org.jibx.runtime does not exist
import org.jibx.runtime.JiBXException;
^
example25\Test.java:25: error: cannot find symbol
IBindingFactory bfact = BindingDirectory.getFactory(Order.class);
^
symbol: class IBindingFactory
location: class Test
example25\Test.java:25: error: cannot find symbol
IBindingFactory bfact = BindingDirectory.getFactory(Order.class);
^
symbol: variable BindingDirectory
location: class Test
example25\Test.java:26: error: cannot find symbol
IUnmarshallingContext uctx = bfact.createUnmarshallingContext();
^
symbol: class IUnmarshallingContext
location: class Test
example25\Test.java:40: error: cannot find symbol
IMarshallingContext mctx = bfact.createMarshallingContext();
^
symbol: class IMarshallingContext
location: class Test
example25\Test.java:52: error: cannot find symbol
} catch (JiBXException e)
^
symbol: class JiBXException
location: class Test
10 errors
Test所使用的所有類文件都位於其旁邊,並已正確編譯。 測試是最后一個給我帶來麻煩的文件。 此外,測試中使用的一些類存在於C:\\ jibx \\ lib>中,與我執行命令所在的C:\\ jibx \\ tutorial>相反。 關於如何cld解決此問題而不修改我以前生成的類文件的任何建議,將不勝感激。
將目錄C:\\ jibx \\ lib添加到您的類路徑中,例如
SET CLASSPATH=%CLASSPATH%;C:\jibx\lib;.
然后做一個javac
由於存在一些導入錯誤,如Satya所述,正確設置了類路徑,並且您當前的類位於某個目錄中,因此應使用javac的-d屬性對其進行編譯,如下所示
javac -d。 YourfileName.java
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.