Java & Linux 问题



想知道是否有人可以帮助弄清楚为什么这不想在Linux上运行。因为此更新程序所针对的游戏是为 Mac、Linux 和 Windows 制作的,因此试图让它每个人都可以毫无问题地使用它。

该程序在窗口上完美运行,没有一个错误。我的朋友使用 Linux 运行它,它只需要什么,它只是检测文件夹并停止。

package mu;
import java.awt.Component;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JProgressBar;
public class Main extends JFrame
{
    private static final long serialVersionUID = 2627728992434582295L;
    public String site;
    public String filename;
    public static String[] mods = new String[99];
    public static String[] Cver = new String[99]; //Current Version
    public static String[] webs = new String[99]; //Web Address to file
    public static String[] Nver = new String[99]; //New Version
    public static int num = 0;
    public static final void main(String[] args) throws Exception
    {
        folders();
    }
    public static void folders(){
        File dir = new File("").getAbsoluteFile();
        File[] files = dir.listFiles();
        FileFilter filefilter= new FileFilter(){
            public boolean accept(File file){
                return file.isDirectory();
            }
        };
        files = dir.listFiles(filefilter);
        System.out.println(files.length + " Mods found!");
        if(files.length == 0){
        }else{
            for(int i=0; i<files.length; i++){
                File filename = files[i];
                Freader(filename);
            }
        }
    }
    public static void Freader(File dir){
        File f = new File(dir + "\version.txt");
        if(f.exists()){
            try{
                FileReader in = new FileReader(f);
                BufferedReader br = new BufferedReader(in);
                String str;
                int line = 0;
                while((str = br.readLine()) != null){
                    if(line == 0){
                        mods[num] = str;
                        System.out.println(mods[num]);
                    }else if(line == 1){
                        Cver[num] = str;
                        System.out.println(Cver[num]);
                    }else if(line == 2){
                        webs[num] = str;
                        System.out.println(webs[num]);
                        Oreader(webs[num]);
                    }
                    line++;
                }
                num++;
                br.close();
            }catch(IOException e){
                e.printStackTrace();
            }
        }else{
        }
    }
    public static void Oreader(String dir){
        try{
            URL url = new URL(dir);
            BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
            String str;
            int line = 0;
            boolean newVer = false;
            while((str = in.readLine()) != null){
                if (line == 0){
                    Nver[num] = str;
                    if(!Cver[num].equals(Nver[num])){
                        System.out.println(Nver[num]);
                        System.out.println("New version available!");
                        newVer = true;
                    }
                }else if(newVer && line == 1){
                    DL(str, (mods[num]+Nver[num].toString() + ".zip"));
                }
                line++;
            }
            in.close();
        }catch(MalformedURLException e){
            e.printStackTrace();
        }catch(IOException e){
            e.printStackTrace();
        }
    }
    public static void DL(String web,String name){
        File dir = new File("").getAbsoluteFile();
        float Precent = 0;
        JFrame frm = new JFrame();
        JProgressBar current = new JProgressBar(0,100);
        current.setSize(394,25);
        current.setValue(0);
        current.setStringPainted(true);
        frm.setTitle("Mod Updater");
        frm.add(current);
        JLabel label1 = new JLabel(mods[num],JLabel.CENTER);
        frm.add(label1).setBounds(0, 10, 394, 50);
        frm.setSize(400,100);
        frm.setLayout(null);
        frm.setLocationRelativeTo(null);
        frm.setDefaultCloseOperation(EXIT_ON_CLOSE);
        frm.setResizable(false);
        frm.setVisible(true);
        String site = web;
        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(name);
            java.io.BufferedOutputStream bout = new java.io.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);
                Precent=(totalDataRead*100)/filesize;
                current.setValue((int)Precent);
                if(Precent == 100){
                    extract(dir + "\" +name);
                }
            }
            bout.close();
            in.close();
        }catch(Exception e){
            javax.swing.JOptionPane.showConfirmDialog((Component)null,e.getMessage(),"Error", javax.swing.JOptionPane.DEFAULT_OPTION);
        }
    }
    public static void extract(String filePath){
            File dir = new File("").getAbsoluteFile();
            FileInputStream fis = null;
            ZipInputStream zipIs = null;
            ZipEntry zEntry = null;
            boolean dirs = false;
            try {
                fis = new FileInputStream(filePath);
                zipIs = new ZipInputStream(new BufferedInputStream(fis));
                while((zEntry = zipIs.getNextEntry()) != null){
                    try{
                        byte[] tmp = new byte[4*1024];
                        FileOutputStream fos = null;
                        String opFilePath = dir +"/"+zEntry.getName();
                        if(zEntry.isDirectory()){
                            dirs = new File(zEntry.getName()).mkdirs();
                        }else{
                            System.out.println("Extracting file to "+opFilePath);
                            fos = new FileOutputStream(opFilePath);
                            int size = 0;
                            while((size = zipIs.read(tmp)) != -1){
                                    fos.write(tmp, 0 , size);
                            }
                        }
                        fos.flush();
                        fos.close();
                    } catch(Exception ex){
                        ex.printStackTrace();
                    }
                }
                zipIs.close();
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        System.out.println("Finished");
    }
}

我怀疑你的问题行在这里:

extract(dir + "\" +name);

据我所知,Linux 无法识别其路径中的反斜杠。另一方面,Windows确实可以识别正斜杠。此外,双斜杠是多余的。尝试将该位替换为"/"。

正如@Teeg所建议的那样,更好的是,使用File.pathSeparator()来实现真正的跨平台解决方案。这是他的一句话:

此外,不要硬编码路径分隔符,请使用 File.pathSeparator(),甚至更好的新文件(somePath,文件名)

但这只是一个猜测,我们需要有关您尝试解决的问题的更多信息。

编辑:发现其他一些斜杠错误的地方。不会全部列出。搜索和替换。

最新更新