简体   繁体   English

如何使用javac正确编译此文件

[英]How to properly compile this file using javac

I have the following file called Test.Java the code of which is 我有一个名为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

Here is how i am trying to compile this file and the output that i get 这是我尝试编译此文件以及我得到的输出的方式

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

All the class files used by Test are present next to it and have been properly compiled. Test所使用的所有类文件都位于其旁边,并已正确编译。 Test is the last file that is giving me trouble. 测试是最后一个给我带来麻烦的文件。 Furthermore some of the classes used in test are present in C:\\jibx\\lib> as opposed to C:\\jibx\\tutorial> from where i am executing commands. 此外,测试中使用的一些类存在于C:\\ jibx \\ lib>中,与我执行命令所在的C:\\ jibx \\ tutorial>相反。 Any suggestions on how i cld resolve this issue without modifying my previously generated class files would be appreciated. 关于如何cld解决此问题而不修改我以前生成的类文件的任何建议,将不胜感激。

add the directory C:\\jibx\\lib to your classpath eg 将目录C:\\ jibx \\ lib添加到您的类路径中,例如

SET CLASSPATH=%CLASSPATH%;C:\jibx\lib;.

and then do a javac 然后做一个javac

As there are some import errors set the class path properly as Satya said, And the your current class in some directory you should compile it with -d attribute of javac As shown below 由于存在一些导入错误,如Satya所述,正确设置了类路径,并且您当前的类位于某个目录中,因此应使用javac的-d属性对其进行编译,如下所示

javac -d . javac -d。 YourfileName.java YourfileName.java

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM