IntelliJ: cannot launch a java application using the command line - java

Let me summarize the problem:
An exercise asked me to launch a java application using the Windows 10 Command Line.
It taught me on a video how to add the IntelliJ path to the Environment Variables, so that, whenever I typed java Main I would get my Main.java/class application back.
The first problem that I had stumbled upon was that, instead of using java Main, I'd have to type java Main.java. I thought that it was due to a recent build for the JDK (v14.02), so I let that one pass.
The second problem that I had encountered was that, when the exercise asked me to launch an application using the package directories, the command line would return the following error: Error: Could not find or load main class com.pluralsight.organized.Main. Caused by: java.lang.ClassNotFoundException: com.pluralsight.organized.Main.

Revert whatever damage you've done to the system.
Install the Oracle JDK.
Put the Oracle JDK bin folder on your PATH variable.
Compile your code using javac Main.java
Run your compiled code using java Main
for the ones in the packages, you need to be at the package root, or have a class path to it. For example, let's consider a class com.example.Main
The folder structure should be:
D:/SomePath/com/example/Main.class
Then you need to be at D:/SomePath/ to execute java com.example.Main.
Alternatively, you could be anywhere else and execute java -cp D:/SomePath com.example.Main.

Related

unable to compile and run a java program with multiple source files on the command line

I am trying to compile and run a java program that has 3 source files, and I am not able to get it to run.
I have a folder with 3 java source files, Tester.java, CommandHandler.java, and DualList.java. Tester.java contains the main function, and depends on CommandHandler.java, which depends on DualList.java
I tried to compile the program with javac Tester.java CommandHandler.java DualList.java. It created the three class files, but when I try to run the program with java -cp . Tester I get the following error:
Error: Could not find or load main class Tester
Caused by: java.lang.NoClassDefFoundError: cmsc420_s22/Tester (wrong name: Tester)
I can see that the class file is there, and I got no errors during compilation, so I'm not sure what is going on. I think it might be a problem with my java installation or my configuration. I'm doing this on a kali linux WSL distribution. I haven't changed anything about my installation, and I set my classpath to the current folder so I'm not sure what's wrong
Thanks

The error I get Which checking Apache Pig Version "Error: Could not find or load main class C:\java\lib\tools.jar"

I have my java working properly and also the environment variable is also set for java jdk 1.8.0_261
But when I run the pig -version its shows Error: Could not find or load main class C:\java\lib\tools.jar.
I have also checked the java folder in lib for tools.jar and it is there.
Also the Apache Pig configuration is also correct in pig.cmd
My Hadoop and MapReduce all are working fine with no error.
Your installation is broken, or you have messed up a shell script. That error cannot really mean anything else.
When invoking java.exe, you can either pass a jar file, via java -jar C:\foo.jar, or a classpath + fully qualified class name: java -cp C:\foo.jar;C:\bar.jar com.foo.fullyqualified.ClassName.
That error means you've passed C:\java\lib\tools.jar in the position of that classname. That will fail to run on any system and regardless of installations: C:\java\lib\tools.jar is a file path, not a fully qualified class name. That tools.jar exists in that location is immaterial: It's as if I told you: "I searched for the book titled "Can of Paint" on your bookshelf and I can't find it", and you going: "That's bizarre, because (pointing at the cans of paint on the shelf in the garage), there are cans of paint right here". Java isn't treating that as a path to look up; it is treating it as a class name to search for on the classpath.
Note that this trivial invocation:
java.exe C:\lib\tools.jar
produces the exact error you are observing and that is the correct behaviour of java (because the right way to run it is either java -jar C:\lib\tools.jar or more likely java -cp C:\lib\tools.jar;. com.foo.YourApp. The fix is to.. not invoke java incorrectly like this.
Reasons
Perhaps pig.cmd is broken. The image you pasted does not include the actual call to java.
Perhaps java is broken. Does java.exe work at all? Make some test file, compile it, run it. Does that work?
Perhaps the script is trying to start the jar by letting windows do it (so, double clicking it, or in a script, start foo.jar, which will then end up checking the HKLM registry section for .jar, and then checking the key there for how to start it, and if you've manually edited that, that would break things: You then presumably set it for e.g. java %1 which is broken (it'd have to be java -jar %1).
pig -version doesn't invoke the pig.cmd you think it invokes, but something else.
Something in pig.cmd starts another script. Then that script is broken, or isnt the script pig.cmd thought it was, etc.

Could not find or load main class Javac

Quick note, I went through every single other question through here and nothing seems to work.
So, here is my issue. All I want to do is create a windows batch script that will be used to execute my selenium project on Jenkins. Sounds simple right ? Its probably is, but I am missing something...
Here is my project https://github.com/Daviditooe/Nomad
First command I tried:
javac src/nomad/execute/Execute.java
Execute.java:9: error: package nomad.sites does not exist import nomad.sites.MmaShare;
It also couldnt find any of the jars, so I add all of them
Then I tried:
javac -cp Jars\* src\nomad\execute\Execute.java
This fixed the jars issue but the package not found still exists
So then I tried compiling every single package at the same time
javac -cp Jars\* src\nomad\execute\*.java src\nomad\actions\*.java src\nomad\baseactions\*.java src\nomad\browsers\Chrome.java src\nomad\directory\*.java src\nomad\scripts\*.java src\nomad\sites\*.java src\nomad\urltools\*.java
So, now its not crashing, then I tried compiling...
java src\nomad\execute\Execute
And its giving me Could not find or load main class
So That last thing I tried was compile all of them at the same time.
java src\nomad\actions\MmaShareActions src\nomad\baseactions\BaseActions src\nomad\browsers\Chrome src\nomad\directory\Directory src\nomad\execute\Execute src\nomad\scripts\Vpn src\nomad\sites\MmaShare src\nomad\urltools\UrlTools
Still no luck... anything thoughts would be appreciated.
Again: You must go into the src folder before you execute javac and java!
I can compile and execute it on my Linux Laptop:
stefan#stefanpc:/hdd/stefan/Downloads/Nomad-master/src$ javac -cp '../Jars/*' nomad/execute/*.java nomad/actions/*.java nomad/baseactions/*.java nomad/browsers/Chrome.java nomad/directory/*.java nomad/scripts/*.java nomad/sites/*.java nomad/urltools/*.java
Note: nomad/baseactions/BaseActions.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
stefan#stefanpc:/hdd/stefan/Downloads/Nomad-master/src$ java -cp '../Jars/*:.' nomad.execute.Execute
Exception in thread "main" java.lang.IllegalStateException: The driver executable does not exist: /hdd/stefan/Downloads/Nomad-master/src/C:\Users\Davidito\Desktop\AutoLinks\Jars\chromedriver.exe
Of course my Linux machine does not have the chromedriver.exe installed.
Under Windows, you must use "\" instead of "/" and you must use ";" instead of ":". Note that you must add the current folder "." to the class path when you execute the program. Otherwise java would only search in the Jars folder.

Why can't I run java from the command line anymore?

I've been running java programs from the command line for 5+ years using the same process:
javac Program.java
java Program
This is suddenly not working. Compiling works, but when I try to run the program I get an error saying the class cannot be found.
However, now when I type this, it works:
javac Program.java
java Program.java
This is fine, but when I have a class that extends another, it isn't being found.
Here is how it looks on the command line:
C:\Drive\Java>javac Program.java
C:\Drive\Java>java Program
Error: Could not find or load main class Program
Caused by: java.lang.ClassNotFoundException: Program
C:\Drive\Java>java Program.java
Hello World
Is there something I could have done to cause this change? I noticed it started happening yesterday after a windows update.
The Class's name should be exactly the same with the file name, please paste the code or check it yourself.
I was able to resolve this issue. The problem was that I had added a CLASSPATH environmental variable that pointed to my java bin folder and this was causing the behavior. I deleted it and the problem was resolved.
I suggest you check if your computer system is compatible with your jdk version.
And support? x86 x64

Running a java.class file from Linux command line - Oracle JDK8 Installed - openJDK removed

So, I have removed openjdk from my new Ubuntu system and have installed Oracle JDK 8 and Eclipse from their respective websites. I can run a program from Eclipse, however I cannot run it from the command line. I am also not used to using Eclipse (I use NetBeans for my Java class in college.) I noticed that there is no build button in Eclipse. With all of that being said, here is my command line code:
wil#wil-Aspire-E5-521:~/eclipse-workspace/wiltest/src/wiltest$ ls
test.class test.java
wil#wil-Aspire-E5-521:~/eclipse-workspace/wiltest/src/wiltest$ java wiltest.test.java
Error: Could not find or load main class wiltest.test.java
wil#wil-Aspire-E5-521:~/eclipse-workspace/wiltest/src/wiltest$ java wiltest.testError: Could not find or load main class wiltest.test
wil#wil-Aspire-E5-521:~/eclipse-workspace/wiltest/src/wiltest$ java test
Error: Could not find or load main class test
wil#wil-Aspire-E5-521:~/eclipse-workspace/wiltest/src/wiltest$
You need to add the -classpath . command line option.
You can learn more about classpaths here
I believe the command java -classpath . test might work, but it really depends on a number of items that are better explained in the link above.
I actually answered part of my question myself. (For anybody wondering, the classpath is set to be in present working directory by default.) Anyways, I was running java wiltest.test from src and not bin. However, I ran from binary file and it worked. BUT I deleted the .class file thinking that I could change the source file, recompile using javac, and it would create another test.class in the binary folder. It did not. -sigh-

Categories

Resources