Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从另一个类获取字符串并将其传输到url_Java_String_Class_Public - Fatal编程技术网

Java 从另一个类获取字符串并将其传输到url

Java 从另一个类获取字符串并将其传输到url,java,string,class,public,Java,String,Class,Public,我需要从另一个类获取字符串,如: 名为url的字符串在下面的类2中确定,我需要获取它并复制到copyUrlToFile(url,result+“”\plugins\TentMod“)文件夹,其中url是我需要从另一个类获取的字符串,结果是从文件夹选择器获取的文件夹路径 第一类 package me.ArsenArsen.TentName.installer; import java.io.File; public class TMInstaller { protected Shel

我需要从另一个类获取字符串,如: 名为url的字符串在下面的类2中确定,我需要获取它并复制到copyUrlToFile(url,result+“”\plugins\TentMod“)文件夹,其中url是我需要从另一个类获取的字符串,结果是从文件夹选择器获取的文件夹路径

第一类

package me.ArsenArsen.TentName.installer;

import java.io.File;

public class TMInstaller {

    protected Shell shlTentmodInstaller;
    private Text txtInstallerHasStarted;

    /**
     * Launch the application.
     * @param args
     */
    public static void main(String[] args) {
        try {
            TMInstaller window = new TMInstaller();
            window.open();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Open the window.
     */
    public void open() {
        Display display = Display.getDefault();
        createContents();
        shlTentmodInstaller.open();
        shlTentmodInstaller.layout();
        while (!shlTentmodInstaller.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
    }

    /**
     * Create contents of the window.
     */
    protected void createContents() {
        shlTentmodInstaller = new Shell();
        shlTentmodInstaller.setImage(null);
        shlTentmodInstaller.setTouchEnabled(true);
        shlTentmodInstaller.setSize(450, 228);
        shlTentmodInstaller.setText("TentMod Installer");

        Button btnNewButton = new Button(shlTentmodInstaller, SWT.NONE);
        btnNewButton.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseDown(MouseEvent e) {
                DirectoryDialog dialog = new DirectoryDialog(shlTentmodInstaller, SWT.OPEN);
                dialog.setFilterPath("c:\\");
                dialog.setText("Select your server folder");
                String result = dialog.open();
                boolean success = (new File(result + "//" + "TentMod")).mkdirs();
                String textboxtxt = txtInstallerHasStarted.getText();
                if (!success) {
                    txtInstallerHasStarted.setText(textboxtxt + "\r\nMaking folder FAILED! Did folder exist???");
                } else {
                    txtInstallerHasStarted.setText(textboxtxt + "\r\nMaking folder SUCESS\r\nDownloading files!");
                    try {
                        URL url = new URL(Adv_Settings.class.urls);
                        File destination = new File(result + "plugins\\TentMod\\tent.schematic");
                        FileUtils.copyURLToFile(url, destination);
                    } catch (IOException e1) {
                        StringWriter sw = new StringWriter();
                        PrintWriter pw = new PrintWriter(sw);
                        e1.printStackTrace(pw);
                        String err = sw.toString();
                        txtInstallerHasStarted.setText(txtInstallerHasStarted.getText() + "\r\nINTERNAL ERROR\r\nWIEW STACK TRACE:\r\n" + err);
                    }
                }
            }

        });

        btnNewButton.setBounds(10, 10, 414, 33);
        btnNewButton.setText("Install TentMod");
        String currentDir = System.getProperty("user.dir");
        txtInstallerHasStarted = new Text(shlTentmodInstaller, SWT.BORDER | SWT.READ_ONLY | SWT.MULTI);
        txtInstallerHasStarted.setText("Current Working Directory is: " + currentDir + "\r\nInstaller has started...\r\nWaiting for command...");
        txtInstallerHasStarted.setBounds(10, 49, 414, 104);

        Button btnNewButton_1 = new Button(shlTentmodInstaller, SWT.NONE);
        btnNewButton_1.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseDown(MouseEvent e) {
                Adv_Settings config = new Adv_Settings();
                config.open();

            }
        });
        btnNewButton_1.setBounds(10, 159, 127, 25);
        btnNewButton_1.setText("Settings (ADVANCED)");

        Button btnExit = new Button(shlTentmodInstaller, SWT.NONE);
        btnExit.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseDown(MouseEvent e) {
                shlTentmodInstaller.close();
            }
        });
        btnExit.setBounds(349, 159, 75, 25);
        btnExit.setText("Exit");

    }
}
我需要第二类的公共字符串URL:

package me.ArsenArsen.TentName.installer;

import org.eclipse.swt.widgets.Display;

public class Adv_Settings {
    private Text text;
    public String urls = new String("http://arsenovic.host56.com/tent.schematic");
    public String jar_url = new String("http://dev.bukkit.org/bukkit-plugins/tentmod/files/");
    private Text text_1;

    /**
     * Launch the application.
     * @param args
     */
    public static void main(String[] args) {
        try {
            Adv_Settings window = new Adv_Settings();
            window.open();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Open the window.
     */
    public void open() {
        Display display = Display.getDefault();
        Shell shell = new Shell();
        shell.setSize(276, 134);
        shell.setText("Advanced Settings - TentMod Installer");


        Button btnApply = new Button(shell, SWT.NONE);
        btnApply.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseDown(MouseEvent e) {
                if(text.getText().equals("")){
                    urls = "http://arsenovic.host56.com/tent.schematic";
                } else urls = text.getText();
                if(text_1.getText().equals("")){
                    jar_url = "http://dev.bukkit.org/bukkit-plugins/tentmod/files/";
                } else jar_url = text.getText();
            }
        });
        btnApply.setBounds(91, 64, 75, 25);
        btnApply.setText("Apply");

        Button btnOk = new Button(shell, SWT.NONE);
        btnOk.setBounds(10, 64, 75, 25);
        btnOk.setText("Ok");

        Button btnCancel = new Button(shell, SWT.NONE);
        btnCancel.setBounds(172, 64, 75, 25);
        btnCancel.setText("Cancel");

        text = new Text(shell, SWT.BORDER);
        text.setBounds(10, 10, 237, 21);
        text.setMessage("Insert your specific URL");

        text_1 = new Text(shell, SWT.BORDER);
        text_1.setBounds(10, 37, 237, 21);
        text_1.setMessage("Insert your specific URL for JAR file");

        shell.open();
        shell.layout();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
    }
}

字符串是公开的,这是swt主要使用apache的bit开发来下载文件。

将URL字符串设置为静态变量,并以className.variableName的形式访问变量

public class Adv_Settings {
        private Text text;
        public static String urls = new String("http://arsenovic.host56.com/tent.schematic");
        public String jar_url = new String("http://dev.bukkit.org/bukkit-plugins/tentmod/files/");
        private Text text_1;
         ...
    }
public class TMInstaller {

    protected Shell shlTentmodInstaller;
    private Text txtInstallerHasStarted;

protected void createContents() {
        .....
        URL url = new URL(Adv_Settings.urls);

}

甚至没有问号。@Smutje这并不意味着它不是问题。你想将名为URL的字符串转换为URL类对象吗?是@Jayaprasad,字符串在另一个类中。为什么不能使用URL类的构造函数?URL(字符串)…它是否可以通过文本框编辑?我不确定你的代码是什么,以及你的代码是如何工作的。由于变量是静态的,它可能会更改。除非变量被标记为final,否则它可以更改。具体到你的评论,是的,它可以通过文本框编辑。因此@Jayaprasad U被标记为working:D