简体   繁体   中英

How to export terminal ubuntu output txt file using java

I would ask you something, how to export terminal output to txt file using java programming. Because I want to test log automattically using webdriver this is my code.

public class Test5 
{
    private static WebDriver webDriver;

    public static void main(String[] args) throws AWTException, InterruptedException, JSchException, IOException 
    {
        String host = "host";
        String user = "user";
        String password = "password";
        String command1 = "ssh app@host.com";

        String userPC = System.getProperty("user.name");
        String path = "/home/" + userPC + "/Desktop/ScreenShot/Downloads/";
        FirefoxProfile profile = new FirefoxProfile();
        profile.setPreference("browser.download.folderList", 2);
        profile.setPreference("browser.download.dir", path);
        profile.setPreference("browser.helperApps.neverAsk.saveToDisk",
                "application/pdf, text/csv, application/csv, application/octet-stream, text/plain, text/pdf, application/vnd.google-earth.kml+xml");
        webDriver = new FirefoxDriver(profile);

        Link links = Link.trd;

        webDriver.get("http://app." + links + ".ams:8080/admin/loginservice.do");



        Thread.sleep(1500);
        webDriver.close();

        java.util.Properties config = new java.util.Properties();
        config.put("StrictHostKeyChecking", "No");
        //config.put("PreferredAuthentications", "password");
        JSch jsch = new JSch();
        com.jcraft.jsch.Session session = jsch.getSession(user, host, 22);
        session.setPassword(password);
        session.setConfig(config);
        //session.setConfig("PreferredAuthentications", "password");
        session.connect();

         Channel channel = session.openChannel("shell");
            channel.connect();
        Expect expect = new ExpectBuilder()
                .withOutput(channel.getOutputStream())
                .withInputs(channel.getInputStream(), channel.getExtInputStream())
                .withEchoOutput(System.out)
                .withEchoInput(System.err)
                .build();

        expect.sendLine("less /app/logs/jboss/server.log > qwetest103.txt");
        Thread.sleep(1000);


        PrintStream out = new PrintStream(new FileOutputStream("/home/"+userPC+"/Desktop/ScreenShot/Log/text.txt"));
        System.setOut(out);

        String format = "jpg";


        Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
        BufferedImage screenFullImage = robot.createScreenCapture(screenRect);
        ImageIO.write(screenFullImage, format, new File("/home/"+userPC+"/Desktop/ScreenShot/Log/Log"+new Date()+".png"));


    }
}

can you help my to solve my problem?

Regards

that's quite easy. Write the below code:

PrintStream out = new PrintStream(new FileOutputStream("/urlocation/output.txt"));
System.setOut(out);

This will save ur console output to a text file named output.txt. Hope it will help u.

one more thing,

where u put these two line, it will take console log to file after that line. If u put these two line at top, it will capture all log from there to file . write these two line before declaring webdriver.

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