Java - 编译器将引用视为变量? [英] Java - compiler sees reference as a variable?

查看:63
本文介绍了Java - 编译器将引用视为变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

过去一个月我一直在做一个大学项目,到目前为止一切进展顺利。



我一直用Java编写它,现在我正试图通过编译和运行.class文件来测试我的程序。我在cmd(命令提示符)中编译它。



当我尝试编译它时,它显示4个找不到符号错误,所有声明都是变量。我发现所有这些变量实际上都是我用来调用其他文件中其他类的类名。



以下类名的错误重复, TempFiles,ExplorerHistory,Downloads和RecycleBin。



以下是调用类的代码:

< pre lang =java> if (chckbxTemporaryFilesUser.isSelected()){
TempFiles.deleteTemporaryFiles();
}

if (chckbxFileExplorerUser.isSelected()){
ExplorerHistory.deleteExplorerHistory();
}

if (chckbxDownloadsUser.isSelected()){
Downloads.deleteDownloads();
}

if (chckbxRecycleBinUser.isSelected()){
RecycleBin.deleteRecycleBin();
}





如您所见,代码检查是否选中了某个复选框,然后它调用了一个类,和方法。例如,如果选中下载复选框,则会调用下载类,并在该类中调用deleteDownloads方法。



程序的方式工作,你选择一个复选框,单击一个按钮,并根据你选择的复选框,它将从其他文件调用类,执行所需的程序。





这是我的主类的开头,显示其中的所有方法,以及所有导入(我删除了大部分无关的代码,只留下了方法):

  import  java.awt.event.ActionEvent; 
import java.awt.event.ActionListener;
import java.io. *;
import javax.swing。*;
import java.awt。*;

public class Pither implements ActionListener {

private JFrame框架;
public static void main ( String [] args){

}

public Pither(){
initialize();
}

private void initialize(){

}
@覆盖
public void actionPerformed(ActionEvent e){
/ *
这是调用类的地方。
又名if语句。
* /

}





这里是TempFiles类的开头:

  import  java.io.File; 
import java.io.IOException;

public class TempFiles {
private static final

public static void main( String [] args){
deleteTemporaryFiles();
}
public static void deleteTemporaryFiles(){

}

public static void delete(文件文件)







以下是cmd中显示的确切错误:



 Pither.java:422:错误:找不到symbol 
TempFiles.deleteTemporaryFiles();
^
符号:变量TempFiles
location:class Pither
Pither.java:426:错误:找不到符号
ExplorerHistory.deleteExplorerHistory();
^
符号:变量ExplorerHistory
location:class Pither
Pither.java:430:错误:找不到符号
Downloads.deleteDownloads();
^
符号:变量下载
位置:class Pither
Pither.java:434:错误:找不到符号
RecycleBin.deleteRecycleBin();
^
符号:变量RecycleBin
location:class Pither





if语句是在主班结束时,所以422-434确认是他们造成了错误。





PS请尝试在基本级别进行讨论,因为我还不熟悉Java,并且仅在不到2个月前开始学习它。



< b>我尝试了什么:



我尝试删除在问题首次出现之前添加到代码中的所有内容,但错误仍然存​​在,并且是相同的。



我也尝试一次删除一个代码块,我很肯定调用类的代码导致错误。

解决方案

真正的问题描述隐藏在这条评论中:

引用:

但它确实有效。如果我在像NetBeans或Eclipse这样的IDE中运行它,它工作正常,控制台不会显示任何错误。当我尝试在像cmd这样的终端中编译代码时,它会显示错误。



使用IDE时,你有一些项目管理这有助于组织文件甚至类。 IDE还将执行命令行编译器,但传递其他项目特定参数。



如果你想自己执行编译器,请检查IDE传递的参数,并在手动执行编译器时也传递这些参数。当IDE这样做时,可能还需要设置一些环境变量。


您的代码使用类名,就像它们是变量一样,因此错误消息是正确的。假设你的代码中的某个地方你声明了一个类型 TempFiles 的变量,如:

 TempFiles tempObj =  new  TempFiles(); 

// 那么您的使用应该是:

tempObj.deleteTemporaryFiles();


I've been working on a college project for the past month, and so far everything is going well.

I've been coding it in Java, and right now I'm trying to test my program by compiling it and running the .class file. I'm compiling it in cmd (Command Prompt).

When I try to compile it, it shows 4 "Can't find symbol" errors, all stating a variable. I found that all of these "variables" are actually class names that I use to call other classes in other files.

The error repeats for the following class names, "TempFiles","ExplorerHistory", "Downloads", and "RecycleBin".

Here is the code that calls the classes:

if(chckbxTemporaryFilesUser.isSelected()) {
    TempFiles.deleteTemporaryFiles();
}
		
if(chckbxFileExplorerUser.isSelected()) {
    ExplorerHistory.deleteExplorerHistory();
}
		
if(chckbxDownloadsUser.isSelected()) {
    Downloads.deleteDownloads();
}
	
if(chckbxRecycleBinUser.isSelected()) {
    RecycleBin.deleteRecycleBin();
}



As you can see, the code checks if a certain checkbox is selected, and then it calls a class, and a method. For example, if checkbox "Downloads" is selected, it calls for the class "Downloads", and a method "deleteDownloads" inside that class.

The way the program works, is you select a check box, click a button, and depending on which check boxes you select, it will call the classes from other files, which carry out the required procedure.


Here is the beginning of my main class, showing all the methods in it, and all the imports (I have deleted most of the irrelavent code, and left only the methods):

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import javax.swing.*;
import java.awt.*;

public class Pither implements ActionListener {
	
	private JFrame frame;
	public static void main(String[] args) {

	}

	public Pither() {
		initialize();
	}

	private void initialize() {

}
	@Override
	public void actionPerformed(ActionEvent e) {
        /*
        This is where the classes are being called from.
        aka where the "if" statements are.
        */
}



And here is the beginning of the TempFiles class:

import java.io.File;
import java.io.IOException;

public class TempFiles {
	private static final

public static void main(String[] args) {
	deleteTemporaryFiles();
}
	public static void deleteTemporaryFiles() {

    }

    public static void delete(File file)




Here are the exact errors that display in cmd:

Pither.java:422: error: cannot find symbol
                        TempFiles.deleteTemporaryFiles();
                        ^
  symbol:   variable TempFiles
  location: class Pither
Pither.java:426: error: cannot find symbol
                        ExplorerHistory.deleteExplorerHistory();
                        ^
  symbol:   variable ExplorerHistory
  location: class Pither
Pither.java:430: error: cannot find symbol
                        Downloads.deleteDownloads();
                        ^
  symbol:   variable Downloads
  location: class Pither
Pither.java:434: error: cannot find symbol
                        RecycleBin.deleteRecycleBin();
                        ^
  symbol:   variable RecycleBin
  location: class Pither



The "if" statements are at the end of the main class, so 422-434 confirms that it's them causing the errors.


P.S. Please try to talk at a "basic level", as I'm still pretty new to Java, and have only started learning it less than 2 months ago.

What I have tried:

I tried deleting everything I have added to the code before the issue first occurred, but the errors were still there, and were the same.

I also tried deleting one block of code at a time, and I am positive that the code calling the classes is causing the errors.

解决方案

The real problem description is buried in this comment:

Quote:

But it does work. If I run it in an IDE like NetBeans or Eclipse, it works fine and the console doesn't display any errors. It's when I try to compile the code in a terminal like cmd, that it displays errors.


When using an IDE you have some kind of project management that helps organising files and even classes. The IDE will also execute the command line compiler but pass additional project specific arguments.

If you want to execute the compiler yourself, check which arguments are passed by the IDE and pass these too when executing the compiler manually. It may be also necessary to setup some environment variables when the IDE does so.


Your code is using the class names as if they are variables so the error messages are correct. Assuming that somewhere in your code you have declared a variable of type TempFiles like:

TempFiles tempObj = new TempFiles();

// then your usage should be:

tempObj.deleteTemporaryFiles();


这篇关于Java - 编译器将引用视为变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆