简体   繁体   English

编译Java程序时遇到问题,我是新手。 无法弄清楚如何处理目录

[英]Having trouble compiling a Java program, I am new to it. Can't figure out what to do with directories

I have been searching the web trying to find the answer to my question, but everywhere I look seems to have too complex of a solution for a beginner like me. 我一直在网上试图找到我的问题的答案,但我看到的每个地方似乎都有像我这样的初学者太复杂的解决方案。 I have been working on this project, and just now realized that I should've made a package, or something like that. 我一直在研究这个项目,刚才意识到我应该做一个包,或类似的东西。 The thing is though, my program was working fine until I started dabbling with it, and now it won't work at all. 事情是,我的程序工作正常,直到我开始涉足它,现在它根本不起作用。 I am getting this error: 我收到此错误:

Exception in thread "main" java.lang.NoClassDefFoundError: BubbleSort. class
Caused by: java.lang.ClassNotFoundException: BubbleSort.class
        at java.net.URLClassLoader.findClass(URLClassLoader.java:434)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:672)
        at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:358)
        at ``java.lang.ClassLoader.loadClass(ClassLoader.java:638)
Could not find the main class: BubbleSort.class.  Program will exit.

Here's how my "path" looks, if I am not mistaken. 如果我没有弄错的话,这就是我的“路径”的样子。 I am connected to my school's Z: drive through a remote connection, and from there I have a folder called myFirstname_Lastname_A4, which then leads me to another folder called sortingzz which I believe is supposed to have only my source files, but it also ended up with my class files in there whenever I compiled. 我连接到我学校的Z:通过远程连接驱动,从那里我有一个名为myFirstname_Lastname_A4的文件夹,然后引导我到另一个名为sortingzz的文件夹,我相信它应该只有我的源文件,但它也结束了每当我编译时,我的类文件就在那里。 So here's what I am doing to compile. 所以这就是我要编译的内容。

 cd myFirstname_Lastname_A4/sortingzz

javac *.java (Works fine, this is where I end up with my Class files inside of my sortingzz folder) javac *.java (工作正常,这是我最终在我的sortingzz文件夹中的Class文件)

java * (This is where I get the error) java * (这是我收到错误的地方)

I am pretty sure I am just trying to run the program wrong. 我很确定我只是试图运行程序错误。 Before I started messing around with stuff I wasn't ready for, I used to just run the file with my main function in it, like this 在我开始讨论我还没准备好的东西之前,我曾经只用我的main函数运行文件,就像这样

javac SortingImplementation.java

java SortingImplementation

And that for the most part worked fine, but I started having trouble calling certain classes from other classes, so thats when I found out I was suppose to do the packaging and importing stuff. 而且大多数情况下工作正常,但我开始无法从其他类调用某些类,所以当我发现我想要做包装和导入东西时。

In case that is the issue, I have on the top line of every source file: package sortingzz; 如果这是问题,我在每个源文件的顶线:package sortingzz;

and I am importing like this: 我这样导入:

import sortingzz.* ; import sortingzz.* ;

This is correct, right? 这是对的,对吗?

UPDATE: 更新:

I decided to give up on class pathing and trying to package everything, because as usual, I am getting responses that are way over my head, and to be honest I don't think it is necessary. 我决定放弃上课,试图打包所有东西,因为像往常一样,我得到的反应是我的头脑,老实说我不认为这是必要的。

After removing package and importing from everything, and once again compiling using javac *., it actually compiles this time. 删除包并从所有内容导入后,再次使用javac *。进行编译后,它实际上编译了这一次。 However whenever I try to run my class with the main in it, SortingImplementation, it tells me that 但是每当我尝试使用main中的main运行我的类时,SortingImplementation就会告诉我

Could not find the main class: SortingImplementation.  Program will exit.

I don't get it, I am looking at the SortingImplementation.class right now, with all the other classes and java files, so I am not sure what it's trying to do. 我不明白,我现在正在查看SortingImplementation.class,包含所有其他类和ja​​va文件,所以我不确定它正在尝试做什么。

javac *.java is fine. javac *.java很好。 This will compile your files. 这将编译您的文件。 However, you only need to run the file with your main method in it: java MainClass 但是,您只需要使用main方法运行该文件: java MainClass

You say that you are using packages to organize the classes. 您说您正在使用包来组织类。 In this case you need to set the class path using the -cp flag. 在这种情况下,您需要使用-cp标志设置类路径。

javac -cp /path/to/parent/of/package classname.java
and
java classname

Also, your main class should be declared public and should have a main() 此外,您的主类应该声明为public并且应该有一个main()

NoClassDefFoundError occures when a class was recognised in compile time but was not available during runtime. 在编译时识别类但在运行时期间不可用时,会发生NoClassDefFoundError。 So the JVM can't find your class in the classpath. 因此JVM无法在类路径中找到您的类。

using -cp flag to specify where your package is should work. 使用-cp标志指定包应该在哪里工作。

the commanc javac *.java compiles all found java files to corresponding .class files. commanc javac *.java将所有找到的java文件编译为相应的.class文件。 If you all your classfiles are in the same folder, which they should, you just run your regular java SortingImplementation command. 如果您的所有类文件都在同一个文件夹中,那么您只需运行常规的java SortingImplementation命令即可。

java * would, a bit depending on your OS, yield in an undesired command. java *会根据您的操作系统而有所不同,会产生不希望的命令。 For instance, on Linux it would be expanded by the OS to java SortingImplementation.java SortingImplementation.class BubbleSort . 例如,在Linux上它将由OS扩展为java SortingImplementation.java SortingImplementation.class BubbleSort The last one is a directory, which ofcourse is not an executable class. 最后一个是目录,其中的目录不是可执行类。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java Udemy 课程闰年计算器 - 无法弄清楚我做错了什么 - Java Udemy course leap year calculator - can't figure out what am I doing wrong 程序未针对Java进行编译我缺少什么 - Program is not compiling for Java what am I Missing 我无法弄清楚如何处理我收到的错误。 这是我的购物车项目 - I am having trouble figuring out what to do with the error I receive. This is my Shopping Cart project 嗨,我是 Java 的新手,我在试图找出底部“case SCISSORS:”的问题时遇到了麻烦,有人可以帮我:) - Hi, im new to Java and im having trouble trying to figure out the problem with “case SCISSORS:” at the bottom can somebody help me:) 无法弄清楚如何在Java博士中为我的Java程序进行try Catch异常? - Can't Figure Out How To Do a Try Catch Exception for my Java Program in Dr. Java? 空指针异常,我无法弄清楚我在做什么错 - Null Pointer Exception, I can't figure out what I am doing wrong 我在运行 Java 应用程序时遇到了问题,但它无法按预期执行 - I am having trouble when I run the java application and it can't execute as I expect 我无法弄清楚这个线程有什么问题,我试图理解多线程 - I can't figure out what is wrong with this threads, I am trying to understand multithreading NullPointerException被抛出,但我不知道如何解决它。 - A NullPointerException is being thrown but I can't figure out how to fix it. 无法弄清楚我在此方法中做错了什么(compute_even) - Can't figure out what I am doing wrong in this method ( compute_even )
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM