Pregunta

I'm currently working on an application that generates and validates .xml files based on some template files.

The method I call to generate those files goes like this:

FtlProcessingController ctrl = new FtlProcessingController();
ctrl.process(TEMPLATES, OUTPUT_DIRECTORY, root, metadata);

I'm not the author of this FtlProcessingController class, but the process method basically takes the template files from the TEMPLATES directory, fills them with what's inside the root object (a tree of Answer objects) and the metadata object (a Map of additional data), and ouputs the .xml files to OUTPUT_ DIRECTORY.

Briefly, the main code looks like this:

// Prepare files and data

FtlProcessingController ctrl = new FtlProcessingController();
ctrl.process(TEMPLATES, OUTPUT_DIRECTORY, root, metadata);

// Validate XML files

The problem is any console output after the process call does not work; nothing shows up in the console.

I tried surrounding the call with some test output and saving the PrintStream for a reset:

System.out.println("Console");
PrintStream stdOut = System.out;

FtlProcessingController ctrl = new FtlProcessingController();
ctrl.process(TEMPLATES, OUTPUT_DIRECTORY, root, metadata);

// Test Exception was thrown here.

System.setOut(stdOut);
System.out.println("Console again");

but I only get:

Console

I know for sure that there is no infinite loop or System.exit() in this call, since the files are successfully generated. I even tried throwing an Exception right after the call to confirm that, and the Exception was thrown normally.

My question is: independently of what the process method does or doesn't do with System.out, shouldn't I be able to print again on the console after the System.setOut(stdOut) call?

What could be possibly messing up the standard output so that the saved PrintStream doesn't work?

Thanks!

Full main code, for the curious:

public static void main(String[] args) {
    try {
        // Parse answers
        Answer root = AnswerUtils.fromJSON(JSON_FILE, Answer.class);

        ModuleInfo manager = new SimpleModuleInfo(MODULE_MANAGER_ID, MODULE_MANAGER_CATEGORY, MODULE_MANAGER_NAME,
                MODULE_MANAGER_VERSION, MODULE_MANAGER_VERSION);
        ModuleInfo module = new SimpleModuleInfo(MODULE_ID, MODULE_CATEGORY, MODULE_NAME, MODULE_VERSION, INSTANCE);

        List<ModuleInfo> info = new ArrayList<ModuleInfo>();
        info.add(manager);
        info.add(module);
        if (DEPENDENCIES_FILES != null) {
            // property file, with all dependencies as a comma-separated value under the 'dependencies' key
            Properties props = new Properties();
            InputStream in = null;
            try {
                in = new FileInputStream(DEPENDENCIES_FILES);
                props.load(in);
            } finally {
                IOUtils.closeQuietly(in);
            }
            String[] values = StringUtils.split(props.getProperty("dependencies"), ',');
            if (values != null) {
                for (String value : values) {
                    ModuleDependency dep = new ModuleDependency(StringUtils.trim(value));
                    info.add(new SimpleModuleInfo(dep.getIdentifier(), "Category", WordUtils.capitalizeFully(
                            dep.getIdentifier(), new char[] { '-' }), dep.getVersion().toString(), dep
                            .isInstanceDependency() ? module.getInstance() : "Default"));
                }
            }
        }
        Map<String, Object> metadata = getMetadata(HOME, os, arch, module, info.toArray(new ModuleInfo[info.size()]));

        System.out.println("Console");
        PrintStream stdOut = System.out;

        FtlProcessingController ctrl = new FtlProcessingController();
        ctrl.process(TEMPLATES, OUTPUT_DIRECTORY, root, metadata);

        System.setOut(stdOut);
        System.out.println("Console again");

        // Validate XML
        Collection<File> xmlFiles = FileUtils.listFiles(OUTPUT_DIRECTORY, OUTPUT_FILES_EXTENSIONS, WITH_SUBDIRECTORIES);
        for (File file : xmlFiles) {
            System.out.println(file.getAbsolutePath());
            SAXParserFactory factory = SAXParserFactory.newInstance();
            factory.setValidating(false);
            factory.setNamespaceAware(true);
            factory.setFeature(FACTORY_DTD_FEATURE, false);

            SAXParser parser = factory.newSAXParser();

            XMLReader reader = parser.getXMLReader();
            reader.setErrorHandler(new SPErrorHandler());
            reader.parse(file.getAbsolutePath());
        }
        if(true)
            throw new IOException();
    } catch (SAXException se) {
        se.printStackTrace();
    } catch (IOException ioe) {
        ioe.printStackTrace();
    } catch (ParserConfigurationException pce) {
        pce.printStackTrace();
    }
}
¿Fue útil?

Solución

It is possible that the method you call reassigns the System.out stream, which can be done with the System.setOut(PrintStream) method.

If that is the case and output is no longer visible in the Java console it maybe that the output is written to a log file. This approach for logging is, however, an extremely bad idea and one shouldn't do it especially in a library that others may end up using.

An idea even worse than that would be to actually close the System.out stream. I can't think of any scenario justifying doing this.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top