简体   繁体   中英

Run java class from cmd with full path?

I have compile a java file

javac XSDValidator.java

And I get a XSDValidator.class

Lets say I have the the class ( XSDValidatorc.class ) file in

C:\xampp\htdocs\xsd_validtion

And I write this in cmd

C:\xampp\htdocs\xsd_validtion> java XSDValidator students.xsd students.xml

It works fine. But its not working if I I'am in another directory and want to run the file with absolute path. Why doesn't it work?

Lite this, lets say I'am in the directory

C:\aaa\User\Document

And write like this it's not working.

java C:\xampp\htdocs\xsd_validtion\XSDValidator C:\xampp\htdocs\xsd_validtion\students.xsd C:\xampp\htdocs\xsd_validtion\students.xml

This is the java-file

https://www.tutorialspoint.com/xsd/xsd_validation.htm
import java.io.File;
import java.io.IOException;

import javax.xml.XMLConstants;
import javax.xml.transform.stream.StreamSource;

import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;

import org.xml.sax.SAXException;

public class XSDValidator {
   public static void main(String[] args) {
      if(args.length !=2){
         System.out.println("Usage : XSDValidator <file-name.xsd> <file-name.xml>" );
      } else {
         boolean isValid = validateXMLSchema(args[0],args[1]);

         if(isValid){
            System.out.println(args[1] + " is valid against " + args[0]);
         } else {
            System.out.println(args[1] + " is not valid against " + args[0]);
         }
      }
   }

   public static boolean validateXMLSchema(String xsdPath, String xmlPath){
      try {
         SchemaFactory factory =
            SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
            Schema schema = factory.newSchema(new File(xsdPath));
            Validator validator = schema.newValidator();
            validator.validate(new StreamSource(new File(xmlPath)));
      } catch (IOException e){
         System.out.println("Exception: "+e.getMessage());
         return false;
      }catch(SAXException e1){
         System.out.println("SAX Exception: "+e1.getMessage());
         return false;
      }

      return true;

   }
}

use the java classpath:

java -cp C:\xampp\htdocs\xsd_validtion\ XSDValidator C:\xampp\htdocs\xsd_validtion\students.xsd C:\xampp\htdocs\xsd_validtion\students.xml

This includes the directory where the class file is located to the classpath.

directories in java are package structures. Thats why you can not use it as a normal Path

The answer from Jens

java -cp C:\xampp\htdocs\xsd_validtion\ XSDValidator C:\xampp\htdocs\xsd_validtion\students.xsd C:\xampp\htdocs\xsd_validtion\students.xml

can be the accepted one. I just want to give another solution, in some situations, it's better

CD C:\xampp\htdocs\xsd_validtion

java XSDValidator students.xsd students.xml

CD C:\aaa\User\Document

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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