Results 1 to 3 of 3
  1. #1
    cjburkey01 is offline Member
    Join Date
    Dec 2013
    Posts
    46
    Blog Entries
    2
    Rep Power
    0

    Angry Download Script Weirdness

    So, I have this code:
    Java Code:
    public static final void downloadMods(String file, File folder) throws Exception {
    	
    	System.out.println("Doing " + file + " from " + "http://cjburkey.com/techpack/mods/" + file);
    	System.out.println("Going to " + folder + "/Techpack/mods/" + file);
    		JFrame frm=new JFrame();
    		JProgressBar current = new JProgressBar(0, 100);
    		current.setSize(50,50);
    		current.setValue(0);
    		current.setStringPainted(true);
    		frm.add(current);
    		frm.setLocationRelativeTo(null);
    		frm.setVisible(true);
    		frm.setLayout(new FlowLayout());
    		frm.setSize(200, 50);
    		frm.setDefaultCloseOperation(EXIT_ON_CLOSE);
    		frm.setResizable(false);
    		frm.setTitle(file);
    		
    		try {
    			URL url=new URL("http://cjburkey.com/techpack/mods/" + file);
    			HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    			int filesize = connection.getContentLength();
    			float totalDataRead=0;
                java.io.BufferedInputStream in = new java.io.BufferedInputStream(connection.getInputStream());
                java.io.FileOutputStream fos = new java.io.FileOutputStream(folder + "/Techpack/mods/" + file);
                java.io.BufferedOutputStream bout = new BufferedOutputStream(fos,1024);
                byte[] data = new byte[1024];
                int i=0;
                while((i=in.read(data,0,1024))>=0) {
                	totalDataRead=totalDataRead+i;
                	bout.write(data,0,i);
                	float Percent=(totalDataRead*100)/filesize;
                	current.setValue((int)Percent);
                }  
                bout.close();
                in.close();
                System.out.println("Done with " + file);
                frm.setVisible(false);
    		} catch(Exception e) {
             javax.swing.JOptionPane.showConfirmDialog((java.awt.Component) null,e.getMessage() + " Not Found", "Error", javax.swing.JOptionPane.DEFAULT_OPTION);
    		}
    	}
    and it doesn't work, but this one does:
    Java Code:
    public static final void downloadInstaller(File folder) throws Exception {
    		
    		String site="http://cjburkey.com/techpack/ForgeInstaller.jar";
    		String filename = "";
    		JFrame frm=new JFrame();
    		JProgressBar current = new JProgressBar(0, 100);
    		current.setSize(50,50);
    		current.setValue(0);
    		current.setStringPainted(true);
    		frm.add(current);
    		frm.setLocationRelativeTo(null);
    		frm.setVisible(true);
    		frm.setLayout(new FlowLayout());
    		frm.setSize(200, 50);
    		frm.setDefaultCloseOperation(EXIT_ON_CLOSE);
    		frm.setResizable(false);
    		frm.setTitle("ForgeInstaller.jar");
    		
    		filename = folder + "/Techpack/ForgeInstaller.jar";
    		
    		try {
    			URL url=new URL(site);
    			HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    			int filesize = connection.getContentLength();
    			float totalDataRead=0;
                java.io.BufferedInputStream in = new java.io.BufferedInputStream(connection.getInputStream());
                java.io.FileOutputStream fos = new java.io.FileOutputStream(filename);
                java.io.BufferedOutputStream bout = new BufferedOutputStream(fos,1024);
                byte[] data = new byte[1024];
                int i=0;
                while((i=in.read(data,0,1024))>=0) {
                	totalDataRead=totalDataRead+i;
                	bout.write(data,0,i);
                	float Percent=(totalDataRead*100)/filesize;
                	current.setValue((int)Percent);
                	System.out.println((int)Percent + "");
                }  
                bout.close();
                in.close();
                System.out.println("Done");
                frm.setVisible(false);
                javax.swing.JOptionPane.showConfirmDialog((java.awt.Component) null,"Download Successful!", "Success!", javax.swing.JOptionPane.DEFAULT_OPTION);
                Runtime.getRuntime().exec("java -jar " + folder + "/Techpack/ForgeInstaller.jar");
                System.exit(0);
    		} catch(Exception e) {
             javax.swing.JOptionPane.showConfirmDialog((java.awt.Component) null,e.getMessage() + " Not Found", "Error", javax.swing.JOptionPane.DEFAULT_OPTION);
    		}
    	}
    What is the difference and why does the first one give the /Users/CJ/Desktop/Techpack/mods/CodeChickenCore 1.3.3.3.jar(file doesn't exist) error? How would I fix this?

  2. #2
    jashburn is offline Senior Member
    Join Date
    Feb 2014
    Posts
    219
    Rep Power
    1

    Default Re: Download Script Weirdness

    It doesn't appear to have anything fundamentally wrong with it. I can get the mod-download code to work, and can reproduce the problem only when I did not create all the necessary output directories/folders. I'm guessing you're missing the "mods" folder within your "Techpack" folder.

    Note, though, that the following is a terrible idea:

    Java Code:
    catch(Exception e) {
      javax.swing.JOptionPane.showConfirmDialog((java.awt.Component) null,e.getMessage() + " Not Found", "Error", javax.swing.JOptionPane.DEFAULT_OPTION);
    }
    If you have replaced it with

    Java Code:
    catch (Exception e) {
      e.printStackTrace();
    }
    you would've probably found the source of the problem based on the stack trace.

    I'd also suggest the following enhancement to automatically create all the necessary output folders (if not already created):

    Java Code:
    java.io.BufferedInputStream in = new java.io.BufferedInputStream(connection.getInputStream());
    
    File outputParent = new File(folder, "/Techpack/mods");
    if (!outputParent.exists()) {
    	boolean parentCreated = outputParent.mkdirs();
    	if (!parentCreated) {
    		System.err.println("ERROR: Can't create " + outputParent);
    		System.exit(1); // terminate here - can't proceed if parent directory not created.
    	}
    }
    
    File outputFile = new File(outputParent, file);
    java.io.FileOutputStream fos = new java.io.FileOutputStream(outputFile);
    
    java.io.BufferedOutputStream bout = new BufferedOutputStream(fos, 1024);
    Finally, you should close all your streams in a 'finally' block.

    Good hunting!

  3. #3
    cjburkey01 is offline Member
    Join Date
    Dec 2013
    Posts
    46
    Blog Entries
    2
    Rep Power
    0

    Default Re: Download Script Weirdness

    Thanks! This code seems to work :)
    Java Code:
    public static final void downloadMods(String file, File folder) throws Exception {
    	
    	System.out.println("Doing " + file + " from " + "http://cjburkey.com/techpack/mods/" + file);
    	System.out.println("Going to " + folder + "/Techpack/mods/" + file);
    		JFrame frm=new JFrame();
    		JProgressBar current = new JProgressBar(0, 100);
    		current.setSize(50,50);
    		current.setValue(0);
    		current.setStringPainted(true);
    		frm.add(current);
    		frm.setLocationRelativeTo(null);
    		frm.setVisible(true);
    		frm.setLayout(new FlowLayout());
    		frm.setSize(200, 50);
    		frm.setDefaultCloseOperation(EXIT_ON_CLOSE);
    		frm.setResizable(false);
    		frm.setTitle(file);
    		
    		try {
    			File outputParent = new File(folder, "/Techpack/mods");
    			if (!outputParent.exists()) {
    			    boolean parentCreated = outputParent.mkdirs();
    			    if (!parentCreated) {
    			        System.err.println("ERROR: Can't create " + outputParent);
    			        System.exit(1);
    			    }
    			}
    			URL url=new URL("http://cjburkey.com/techpack/mods/" + file);
    			HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    			int filesize = connection.getContentLength();
    			float totalDataRead=0;
                java.io.BufferedInputStream in = new java.io.BufferedInputStream(connection.getInputStream());
                java.io.FileOutputStream fos = new java.io.FileOutputStream(folder + "/Techpack/mods/" + file);
                java.io.BufferedOutputStream bout = new BufferedOutputStream(fos,1024);
                byte[] data = new byte[1024];
                int i=0;
                while((i=in.read(data,0,1024))>=0) {
                	totalDataRead=totalDataRead+i;
                	bout.write(data,0,i);
                	float Percent=(totalDataRead*100)/filesize;
                	current.setValue((int)Percent);
                }  
                System.out.println("Done with " + file);
                frm.setVisible(false);
    		} catch(Exception e) {
             //javax.swing.JOptionPane.showConfirmDialog((java.awt.Component) null,e.getMessage() + " Not Found", "Error", javax.swing.JOptionPane.DEFAULT_OPTION);
    		 e.printStackTrace();
    		}

Similar Threads

  1. Weirdness with GUI building
    By Gagnrath in forum New To Java
    Replies: 0
    Last Post: 08-19-2013, 02:16 AM
  2. Replies: 5
    Last Post: 05-24-2013, 07:11 AM
  3. AccessDeniedException weirdness
    By KoalaInRehab in forum New To Java
    Replies: 3
    Last Post: 05-23-2012, 07:21 AM
  4. TreeSet weirdness
    By Bulska in forum New To Java
    Replies: 3
    Last Post: 03-18-2011, 03:29 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •