javaswinguser-interfacejsoupjxl

Running a JAVA Program with a Button (GUI)


I've been stumped on this all day, despite hours of research. Below is a basic idea of my program. It extracts data from links and puts it into a spreadsheet, and it can run for hours at a time. My intent is to connect this to a GUI with a progress bar. I'm trying to have an "upload" button, then a "Run" button.

For the "Run" button, I cannot figure out how to connect the button to running an instance of the below program.

I have attempted putting

App obj= new App();
obj.main(null);

under actionPerformed with no luck. I receive the following error:

Error:(31, 25) java: unreported exception java.lang.Exception; 
    must be caught or declared to be thrown

I understand now that we can't exactly call main functions. But in that case, how can I make my program work with a GUI? The main reason behind this is to be able to create a webapp for it in the future so that I can access it anywhere.

public class App {

private static final int[] URL_COLUMNS = { 0, 4, 9 }; // Columns A, E, J
public static void main(final String[] args) throws Exception {

    Workbook originalWorkbook = Workbook.getWorkbook(new File("C:/Users/Shadow/Desktop/original.xls"));
    WritableWorkbook workbook = Workbook.createWorkbook(new File("C:/Users/Shadow/Desktop/updated.xls"), originalWorkbook);
    originalWorkbook.close();
    WritableSheet sheet = workbook.getSheet(0);
    Cell cell;

    for (int i = 0; i < URL_COLUMNS.length; i++) {
        int currentRow = 1;
        while (!(cell = sheet.getCell(URL_COLUMNS[i], currentRow)).getType().equals(CellType.EMPTY)) {

            String url = cell.getContents();
            System.out.println("Checking URL: " + url);
            if (url.contains("scrapingsite1.com")) {
                String Price = ScrapingSite1(url);
                System.out.println("Scraping Site1's Price: " + Price);
                // save price into the next column
                Label cellWithPrice = new Label(URL_COLUMNS[i] + 1, currentRow, Price);
                sheet.addCell(cellWithPrice);
            }
            currentRow++;
        }
    }

    workbook.write();
    workbook.close();
}

private static String ScrapingSite1 (String url) throws IOException {
    Document doc = null;
    for (int i=1; i <= 6; i++) {
        try {
            doc = Jsoup.connect(url).userAgent("Mozilla/5.0").timeout(6000).validateTLSCertificates(false).get();
            break;
        } catch (IOException e) {
            System.out.println("Jsoup issue occurred " + i + " time(s).");
        }
    }
    if (doc == null){
        return null;
    }
    else{
        return doc.select("p.price").text();
    }
}
}

Solution

  • At a crude guess I would say that the compiler is prompting you to change:

    App obj= new App();
    obj.main(null);
    

    To one of several possibilities, this one based on 'must be caught' (try / catch):

    try {
        App obj= new App();
        obj.main(null);
    } catch(Exception e) {
        e.printStackTrace(); // good fall back if logging not implemented
    }
    

    Edit: For more information, see the Exceptions lesson of the Java Tutorial.