Java 爪哇及;Linux问题

Java 爪哇及;Linux问题,java,linux,Java,Linux,我想知道是否有人能帮我弄明白为什么这在Linux上不起作用。 因为这个更新程序是为Mac、Linux和Windows制作的,所以要让每个人都可以毫无问题地使用它 该程序在windows上完美运行,没有一个错误。 我有一个朋友,他使用Linux来运行它,它只检测文件夹并停止 package mu; import java.awt.Component; import java.io.BufferedInputStream; import java.io.BufferedReader; import

我想知道是否有人能帮我弄明白为什么这在Linux上不起作用。 因为这个更新程序是为Mac、Linux和Windows制作的,所以要让每个人都可以毫无问题地使用它

该程序在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");
    }
}
包mu;
导入java.awt.Component;
导入java.io.BufferedInputStream;
导入java.io.BufferedReader;
导入java.io.File;
导入java.io.FileFilter;
导入java.io.FileInputStream;
导入java.io.FileNotFoundException;
导入java.io.FileOutputStream;
导入java.io.FileReader;
导入java.io.IOException;
导入java.io.InputStreamReader;
导入java.net.HttpURLConnection;
导入java.net.MalformedURLException;
导入java.net.URL;
导入java.util.zip.ZipEntry;
导入java.util.zip.ZipInputStream;
导入javax.swing.JFrame;
导入javax.swing.JLabel;
导入javax.swing.JProgressBar;
公共类主框架
{
私有静态最终长serialVersionUID=2627728992434582295L;
公共网站;
公共字符串文件名;
公共静态字符串[]mods=新字符串[99];
公共静态字符串[]Cver=新字符串[99];//当前版本
公共静态字符串[]webs=新字符串[99];//文件的网址
公共静态字符串[]Nver=新字符串[99];//新版本
公共静态int num=0;
公共静态最终void main(字符串[]args)引发异常
{
文件夹();
}
公共静态无效文件夹(){
File dir=新文件(“”).getAbsoluteFile();
File[]files=dir.listFiles();
FileFilter FileFilter=newfilefilter(){
公共布尔接受(文件){
返回文件.isDirectory();
}
};
files=dir.listFiles(filefilter);
System.out.println(files.length+“Mods found!”);
如果(files.length==0){
}否则{
对于(int i=0;i=0){
totalDataRead=totalDataRead+i;
写(数据,0,i);
进位=(totalDataRead*100)/文件大小;
当前设置值((int)进位);
如果(进位==100){
摘录(目录+“\\”+名称);
}
}
about.close();
in.close();
}捕获(例外e){
showConfirmDialog((组件)null,e.getMessage(),“Error”,javax.swing.JOptionPane.DEFAULT_选项);
}
}
公共静态无效提取(字符串文件路径){
File dir=新文件(“”).getAbsoluteFile();
FileInputStream fis=null;
ZipInputStream zipIs=null;
ZipEntry zEntry=null;
布尔dirs=false;
试一试{
fis=新文件输入流(文件路径);
zipIs=新的ZipInputStream(新的BufferedInputStream(fis));
while((zEntry=zipIs.getNextEntry())!=null){
试一试{
字节[]tmp=新字节[4*1024];
FileOutputStream=null;
字符串opFilePath=dir+“/”+zEntry.getName();
if(zEntry.isDirectory()){
dirs=新文件(zEntry.getName()).mkdirs();
}否则{
System.out.println(“将文件提取到”+opFilePath);
fos=新的FileOutputStream(opFilePath);
int size=0;
而((大小=zipIs.read(tmp))!=-1){
fos.写入(tmp,0,大小);
}
}
fos.flush();
fos.close();
}捕获(例外情况除外){
例如printStackTrace();
}
}
zipIs.close();
}catch(filenotfounde异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
系统输出打印项次(“完成”);
}
}

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

extract(dir+“\\”+名称)

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

更妙的是,正如@Teeg所建议的,使用
File.pathseptor()
实现真正的跨平台解决方案。下面是他的一句话:

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

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


编辑:找到其他几个斜杠错误的地方。不会全部列出。搜索并替换。

到底是什么问题?“不想工作”有点含糊不清。在Linux上运行时,您会收到什么错误消息?什么不起作用?如果我带着20多行代码来找你,并说“这不管用,请修复它”,我希望你的第一个问题是“错误是什么”?这一点在这里也适用。如果你不能更详细地回答这个问题,那么我们也不能。我不会重复别人已经说过的话,因为我想你已经明白了。我只想补充一点,因为您似乎在做很多文件系统的工作,请记住linux和其他操作系统(如windows)之间的差异。例如,Linux是区分大小写的。另外,不要硬编码路径分隔符,使用
File.pathseptor()
,或者更好的
新文件(somePath,filename)
。以此类推。一个“/”就可以了:-)@gyroless,没错,尽管它真的不重要