Java 当 运行 我的程序时找不到或加载主程序 class
Java can't find or load main class when running my program
我有一个简单的程序,它将一个文本文件作为输入,并在其上使用 运行s kandanes 算法。它编译得很好,但是当我尝试 运行 时出现此错误:
Error: Could not find or load main class a1_13132334
它编译并且 运行 在 netbeans ide 上很好,它们是一个 class 文件,但它不会从命令行 运行。这是程序:
import java.io.*;
public class a1_13132334
{
public static void main(String [] args) throws IOException
{
//make sure the user entered a single text file
if(args.length == 0 || args.length > 1){
//display an error if the user didn't enter a file or entered more than one file
System.out.println("You must enter a single file to read from");
//exit the program if they didn't have the correct input
System.exit(0);
}
else
{
//set up strings for reading in the file
String values = "";
String currentLine;
try {
// Generate the file stream to the file
FileInputStream in = new FileInputStream(args[0]);
try {
// Create data input stream to the file stream
BufferedReader myInput = new BufferedReader(new InputStreamReader(in));
try {
// Loop through the file one line at a time
while((currentLine = myInput.readLine()) != null) { // while loop begins here
values += currentLine;
}
}catch (Exception e) {
// If the file is unreadable output error message
System.out.println("Error: " + e);
}
}catch (Exception e) {
// If the file stream is unaccessable
System.out.println("Error: " + e);
}
}catch (Exception e) {
// If the file is unopenable
System.out.println("failed to open file " + args[0]);
System.out.println("Error: " + e);
}
//split the string containing the data of the file into a string array
String [] data = values.split(",");
//create an array of floats to parse the values of the file into and set it to the length of the string array data
float [] vectors = new float[data.length];
//a for loop to go through the data array
for(int i = 0;i< data.length;i++){
//convert the values in the data string array into floats and put them in the vectors array
vectors[i] = Float.parseFloat(data[i]);
}
//set up the floats and ints that are needed for kandanes alogorithm
//an int to store the start index
int maxStartIndex=0;
//an int to store the final index
int maxEndIndex=0;
//a float to store the max sum
float maxSum = Integer.MIN_VALUE;
// a float to store the sum if you add the current index
float cumulativeSum= 0;
//an int to store the start index so far
int maxStartIndexUntilNow=0;
//a for loop to read through the vectors array
for (int i = 0; i < vectors.length; i++) {
//a float to store the current value of the array
float eachArrayItem = vectors[i];
//adding the value of the current index to the sum so far
cumulativeSum+=eachArrayItem;
//checking if adding the current value increases the max sum
if(cumulativeSum>maxSum){
//if it does make the maxsum include the value of the current index
maxSum = cumulativeSum;
//change the initial index to the max start index until now
maxStartIndex=maxStartIndexUntilNow;
//make the final index the current index
maxEndIndex = i;
}
//if the current index doesn't increase the maxsum check if it makes it less than 0
else if (cumulativeSum<0){
//change the inital index to the next index
maxStartIndexUntilNow=i+1;
//reset the cumulative sum
cumulativeSum=0;
}
}
//print out the results
System.out.println("initial index:\t\t\t"+maxStartIndex);
System.out.println("final index:\t\t\t"+maxEndIndex);
System.out.println("Value of max-sub vector:\t"+maxSum);
}
}
}
它是用这个编译的:
javac a1_13132334.java
和运行与此
java a1_13132334 d:vector25.txt
该文件也确实存在并且工作正常。它是大学作业的一部分,因此需要能够从命令行 运行。
当我编译你提供的代码时
javac a1_13132334.java
然后 运行 它与
java a1_13132334 temp.txt
我可以很好地看到程序输出(好吧,我至少得到了一个数字格式异常)。我会说确保在编译后检查 .class 文件并确保您位于正确的目录中。
也许尝试将您的代码示例放入一个新文件中,而不在新位置使用 netbeans,然后尝试编译并 运行 编译它。
如果这没有帮助,您可以参考这个:Java can't find main class
我有一个简单的程序,它将一个文本文件作为输入,并在其上使用 运行s kandanes 算法。它编译得很好,但是当我尝试 运行 时出现此错误:
Error: Could not find or load main class a1_13132334
它编译并且 运行 在 netbeans ide 上很好,它们是一个 class 文件,但它不会从命令行 运行。这是程序:
import java.io.*;
public class a1_13132334
{
public static void main(String [] args) throws IOException
{
//make sure the user entered a single text file
if(args.length == 0 || args.length > 1){
//display an error if the user didn't enter a file or entered more than one file
System.out.println("You must enter a single file to read from");
//exit the program if they didn't have the correct input
System.exit(0);
}
else
{
//set up strings for reading in the file
String values = "";
String currentLine;
try {
// Generate the file stream to the file
FileInputStream in = new FileInputStream(args[0]);
try {
// Create data input stream to the file stream
BufferedReader myInput = new BufferedReader(new InputStreamReader(in));
try {
// Loop through the file one line at a time
while((currentLine = myInput.readLine()) != null) { // while loop begins here
values += currentLine;
}
}catch (Exception e) {
// If the file is unreadable output error message
System.out.println("Error: " + e);
}
}catch (Exception e) {
// If the file stream is unaccessable
System.out.println("Error: " + e);
}
}catch (Exception e) {
// If the file is unopenable
System.out.println("failed to open file " + args[0]);
System.out.println("Error: " + e);
}
//split the string containing the data of the file into a string array
String [] data = values.split(",");
//create an array of floats to parse the values of the file into and set it to the length of the string array data
float [] vectors = new float[data.length];
//a for loop to go through the data array
for(int i = 0;i< data.length;i++){
//convert the values in the data string array into floats and put them in the vectors array
vectors[i] = Float.parseFloat(data[i]);
}
//set up the floats and ints that are needed for kandanes alogorithm
//an int to store the start index
int maxStartIndex=0;
//an int to store the final index
int maxEndIndex=0;
//a float to store the max sum
float maxSum = Integer.MIN_VALUE;
// a float to store the sum if you add the current index
float cumulativeSum= 0;
//an int to store the start index so far
int maxStartIndexUntilNow=0;
//a for loop to read through the vectors array
for (int i = 0; i < vectors.length; i++) {
//a float to store the current value of the array
float eachArrayItem = vectors[i];
//adding the value of the current index to the sum so far
cumulativeSum+=eachArrayItem;
//checking if adding the current value increases the max sum
if(cumulativeSum>maxSum){
//if it does make the maxsum include the value of the current index
maxSum = cumulativeSum;
//change the initial index to the max start index until now
maxStartIndex=maxStartIndexUntilNow;
//make the final index the current index
maxEndIndex = i;
}
//if the current index doesn't increase the maxsum check if it makes it less than 0
else if (cumulativeSum<0){
//change the inital index to the next index
maxStartIndexUntilNow=i+1;
//reset the cumulative sum
cumulativeSum=0;
}
}
//print out the results
System.out.println("initial index:\t\t\t"+maxStartIndex);
System.out.println("final index:\t\t\t"+maxEndIndex);
System.out.println("Value of max-sub vector:\t"+maxSum);
}
}
}
它是用这个编译的:
javac a1_13132334.java
和运行与此
java a1_13132334 d:vector25.txt
该文件也确实存在并且工作正常。它是大学作业的一部分,因此需要能够从命令行 运行。
当我编译你提供的代码时
javac a1_13132334.java
然后 运行 它与
java a1_13132334 temp.txt
我可以很好地看到程序输出(好吧,我至少得到了一个数字格式异常)。我会说确保在编译后检查 .class 文件并确保您位于正确的目录中。
也许尝试将您的代码示例放入一个新文件中,而不在新位置使用 netbeans,然后尝试编译并 运行 编译它。
如果这没有帮助,您可以参考这个:Java can't find main class