Could not find or load main class hello - java

please, I really need some help with this! I'm working on my very first Java program - the classic "hello world" program. I saved the correct script on Notepad as Hello.java in C:\myProjects and used the command line to compile a Hello.class in the same folder. Even though the class is in the current directory, java still could not find it!
Here is a screenshot of the error message
Then I tried to manually set a classpath, even though I shouldn't need to do that, as seen here, but still the command line is not able to run the Hello.java program.
How do I fix this?? I even tried uninstalling and reinstalling my Java JDK altogether.

You need to capitalize Hello
Your command needs to be java Hello
Java is case sensitive.

or you might just install IntelliJ IDEA / Eclipse / NetBeans and enjoy life a little bit more :D
example step-by-step video (and there are tons more of those):
https://www.youtube.com/watch?v=ypLISBPwIWc

Related

Why isn't my code running in VisuaStudioCode?

I downloaded VS code and tried to run a basic java code in it. I have already installed JDK in my system, set the environment variables too and also downloaded the red hat extension for java. The code runs smoothly in cmd prompt.
But when i run the code in VS it says "File not found in the output and "Hello.java is a non project file" .
The same problem is arising in C program too.
What should i do to resolve this?
[Image] [1]: https://i.stack.imgur.com/hh8o0.jpg
In some cases, launch.json cwd configuration is causing these errors. Could you please also share launch.json file.
This thread might help too.
File not found in Output.
That's because you're running tempCodeRunnerfile.java but hello.java was opened. Also you use the extension Code Runner, so the result will be shown in Output.
After cleaning java workspace, open tempCodeRunnerfile.java and click the triangle button(Run Code) to try again.
It's recommended that using the integrated Terminal to run the code instead of Code Runner.
Hello.java is a non project file.
This couldn't be called as an error but just a warning. Press Ctrl+Shift+P to open Command Platte and choose Java: Create Java Project. Coding in project, this warning won't be shown.
Gif tutorial and more information please refer to Creating a source code file.

Trouble executing a jar file

I am trying to run a executable jar file I made in Intellij on JDK 11.0.2 that contains a GUI system. I have java 8 and Java SDK 11.0.2 installed. Whenever I double click the jar file I expect the main gui login screen to come up, but nothing happens. I have tried other options such as trying to run it through cmd (it gave me an access error) and the HKEY_CLASSES_ROOT\jarfile has the command: "C:\Program Files\Java\jdk-11.0.2\bin\javaw.exe" -jar "%1" %* already.
Cant comment quite yet on other peoples posts so ill just submit an answer, tho its gonna be more of a swing and miss probably.
Id recommend opening that jar file with a java decompiler (The one i had in mind is jd-gui) and checking the integrity of the code, file system, and, in general, just check arround for common errors when packaging, maybe you included something you shouldnt in the class and hence why the problem, or its crashing on load cause of a missing reference you had linked to with a relative path which got broken upon moving the file, those are the two big ones that come to mind that you should check first upon opening the file, other than that, i cant provide much more insight, godspeed to you!

run java program from cmd with class imports from jar files

Hey community I have this rookie problem so if anyone could help that would be great :)
So here it goes...
I'm trying to test the stanford NLP parser by trying to run the java demo file which is included in the file you can download from here
My problem is probably on the imports of the classes included in the .jar files..I tried the solutions on some other stackoverflow threads such as doing
javac -cp "jar/path/number/1";"jar/path/number/2" ParserDemo.java
but the
java -cp "jar/path/number/1";"jar/path/number/2" ParserDemo
command didn't work.
I also tried to extract every jar file in a common folder but then I got the bad class file error..
So what's the move here? How can I solve this ?
P.S. please don't propose as a solution to use the online version of the parser.
EDIT: I'm using Windows 8.1 just in case somebody needs to know
Since it was posted as a comment and I don't know if it would be visible for future reference I'm gonna write the solution down here as proposed by the user mlk
I fixed my java command to java -cp ".;jar/path/number/1";"jar/path/number/2" ParserDemo and it worked perfectly!
This is because the current folder (.) was not included in the classpath so Java could not see the ParserDemo.class file.

Setting up Cloud9 IDE to compile and run Java?

I have tried posting this request in Cloud9's forums, but since it never posts, I am hoping someone here could give me a hand. (The forum keeps saying my question is being submitted for review, but then never shows up).
I can't get Java code to compile and run. I've Googled the issue and read other posts in their limited forums, but I can't get it to compile and run a simple Hello World! program. My .java filename matches the class and the code is fine.
I have tried putting my .java files in the following directories:
test/java/
test/src/
I just get this:
bash: src/HelloWorld.java: No such file or directory
I tried this: http://www.programmingforliving.com/2014/09/java-coding-with-cloud9-ide.html
But I just get:
Running HelloWorld.java
Error: Could not find or load main class HelloWorld
Any suggestions? Given I'm essentially a Java noob I am hoping the solution is fairly simple.
Without you pasting in your code it's tough to tell but make sure your class and filename are both titled HelloWorld. Make sure your src folder is in your main workspace folder so your file should be at /home/ubuntu/workspace/src/HelloWorld.java.
Also, this documentation looks pretty similar to the blog you referenced but still worth a look https://docs.c9.io/docs/custom-runners.
I think that Codenvy is much better in java than Cloud9. It provides easy build & run configuration, code completion, project sharing with permissions or factory (creates clone of your project with one click in minutes).
Take a look at official java spring factory. This link opens temporary(personal) java project ready to code, build & run.
Also Codenvy has project wizard that has a lot of project types including java console app (uses maven).
Ok, decided to screw around with it and figured it out... (then saw that Brady Dowling had posted a link that explained in better detail what needed to be changed).
At any rate, I noticed a few things in Cloud9.
Even after making the changes and saving, it would still not compile correctly. I had to close my session of C9 and go back in. Then it would work.
After compiling, and trying to run the program, it wasn't automatically picking the Java runner I made. So if you are running into issues, make sure the Java (or whatever you decided to name it) runner is selected in the console.
You have to compile and run separately. Kind of annoying, but at least it works. Unlike C/C++ in C9, it won't compile and run by simply hitting 'Run'. If you do, it creates an error saying the .class doesn't exist. Simple as pressing F7 to build, then F5 to run.
Anyway, I took screenshots of what I had to change to get it working. Essentially, just the directories it looks for the .java file in (the right arrows) and the directory it looks for/compiles the .class file to (the left arrows). Since I was putting the .java and .class in the same directory (java), I just changed both to 'java'. In the links Brady and myself posted, they used .bin (for .class) and src for (.java).
1st picture is the Builder file (I called it javac) and the 2nd picture is the Runner file (I called it Java).
Anyway, I hope this helps someone out. I am really liking C9 and it perfectly suits my novice needs at the moment. I like the ability to dabble in multiple languages to see what I like without having to switch applications. The real-time live preview HTML/CSS like Brackets is another huge plus for me.
Regards
For what I understand, C9 workspaces don't come with jdk installed. But I always use these commands:
sudo apt-get update
(wait a while for it to finish processing. Then)
sudo apt-get install default-jdk
(Then It will ask you for some permission. Type "Y")
And then it compiles and runs normally using the terminal.
Sometimes you will also need to install a package to get javac.
Example:
sudo apt-get install openjdk-7-jdk
The java command works right of the box. You may java -version to see that it is Ubuntu running. You can also locate the java binary and see that it is amd64. That is all you need to know to download jdk manually from Oracle.com, like
wget --no-check-certificate --no-cookies --header "Cookie: oraclelicense=accept-securebackup-cookie" http://download.oracle.com/otn-pub/java/jdk/8u92-b14/jdk-8u92-linux-i586.tar.gz
tar xf jdk-8u92-linux-i586.tar.gz
and voila, jdk-1.8.0_92/bin/java(c) can be executed! It allows you to compile java files.

java -cp on Windows problem

Can someone please tell me why this java command in a .bat file won't run my java program on WinXP? I have all my jar files in a folder called lib and my class files in a package folder mypackage.
java -cp ".;.\lib\poi-3.7-20101029.jar;.\lib\jsr173_1.0_api.jar;..." mypackage.MyClassWithMain
I have tried all sorts of things to no avail. I get a ClassNotFound error as soon as the program attempts to use some of the classes in the jar files. So, I think there's something wrong with my -cp option. It does find my main().
I want to give this program to someone who doesn't know a thing about computers, so I want them to be able to double-click the .bat file and go.
I wrote everything on a mac without much problem.
I bet the moderator is going to slap me upside the head for this question, but I did search extensively for an answer to this.
Thanks you!
John
Sorry, I should have put in the entire command line:
java -cp ".;.\libs\jsr173_1.0_api.jar;.\libs\poi-3.7-20101029.jar;.\libs\poi-ooxml-3.7-20101029.jar;.\libs\poi-ooxml-schemas-3.7-20101029.jar;.\libs\resolver.jar;.\libs\xbean.jar;.\libs\xbean_xpath.jar;.\libs\xmlbeans-qname.jar;.\libs\xmlpublic.jar" excelsifter.ExcelSifterController
This is all on one line. I tried / instead of \, but that didn't seem to work. Everything I could find on this indicates that for windows you have to use the backslash.
All the dependencies are here, as far as I know. At least my mac doesn't complain when I use essentially the same command.
My directory containing my .bat file contains the excelsifter package (a folder called excelsifter) and the folder libs with all the jar files in it.
Thanks, John
Your starting string looks ok to me, try to check if there are other dependencies in the libraries you use as #Said mentioned. The best way is to search for class your java cannot find, probably you'll find it declaration in some other library you didn't included in your classpath.

Categories