java jar Error: Unable to access from command line - java

I cant execute java jar file from cmd windows 10.
I have generated a jar file using Intellji.
when I run the command Java -jar I got this error
Error: Unable to access jarfile
I tried running the cmd with administration right but same error. the class path is set correctly. the jar file is located in the right directory .
What could be causing the problem?
Thank you

make sure you use
java -jar TestJar.jar
instead of
java -jar TestJar
If this is not your error, the file might not exist or you donĀ“t have permission to open it.

Related

Unable to run Java main class though windows commandline

I have an external jar and I have written 2 classes named 'TestConnector.java' and 'APIExampleUsageTestData.java' and I have placed these 2 files under default package.
I have placed all the 3 artifacts -> externalConnector.jar, TestConnector.java and APIExampleUsageTestData.java into a folder named 'temp'.
I have opened commandline in the temp folder context and compiled the class files with below command and it executed without any errors:
javac -cp "externalConnector.jar" *.java
I can see that there are 2 class files present in the tem folder.
There is a main method in the APIExampleUsageTestData.class and I'm trying to call that class with the below command, but, is failing
When running the below command in 'C:\Users\AppData\Local\Temp\0.15429262750877082' folder context
java -cp "externalConnector.jar" APIExampleUsageTestData
I'm getting error Error: Could not find or load main class APIExampleUsageTestData
When I trying to run the command, without any class path entry to check what error it is throwing:
java APIExampleUsageTestData
It throws error: Error: A JNI error has occurred, please check your installation and try again Exception in thread "main" java.lang.NoClassDefFoundError: com/external/connector/CustomException
It is a Windows Operating System. Could you please let me know how to run the program successfully?
You need both the current directory and the jar file to be on the classpath. So you want (Linux / Mac):
java -cp .:externalConnector.jar APIExampleUsageTestData
Or (Windows)
java -cp .;externalConnector.jar APIExampleUsageTestData

Can't open a java file in terminal

I have java installed but when I try to open it in terminal it doesn't work
I type
java -jar Minecraft.jar
the output I get is
unable to get access to jar file Minecraft.jar
How I could fix this so I can next time just click the app. My operating system is Linux
Minecraft.jar file must be in your current working directory, which seems not being the case. So you either change your working directory to where Minecraft.jar resides or give full path to it
java --jar /your/valid/full/path/to/Minecraft.jar
give the full path to jar or go to directory and run command java -jar Minecraft.jar.

Compiling programs with packages in java using windows command line

I have a .java file which i compiled in a package named "Mypack",using command line as follows
javac -d . file_name.java // The "." specifies the current working directory which was the desktop
so it creates a folder on the desktop named "Mypack"(The package name), in the folder the .class file for my program is placed.Now i did the following
java -classpath "C:\Users\LoRd CuRZon\Desktop\Mypack" file_name // Error Could not find or load main method
Even if i go into the directory "Mypack" and launch command prompt from that directory and try to run the program i still get the same error.
run it as likewise from Desktop,
c:/.../Desktop> java Mypack.file_name
java command requires fully qualified name .
so from desktop run java Mypack.classname
If you have this error:
Error Could not find or load main method
That means you don't write a main method in you code try to write it.
But befor to do those steps:
Fo compiling a programme do this:
java Mypack.file_name
To run it do like this:
java Mypack.classname

Java -cp on linux

I made a program which runs fine on windows. When I moved it over to CentOS, I got this error:
Error: Could not find or load main class org.wbc.WBCController
This is the file setup and .sh on linux:
And this is the file setup and .bat on windows:
Does anybody know what the problem is, and how I can fix it?
Java will respond with this error even if it cannot find the file wbc.jar. I am guessing that that is your problem. You might want to see that your are executing the shell script from within the right working directory.
Check to see if you can run wbc.sh from the console or put this in wbc.sh to make sure it searches for the jar in the same directory as the shell script:
#!/bin/sh
java -cp `dirname $0`/wbc.jar org.wbc.WBCController

Error unable to access jarfile C:\Jar

My jar file P2.jar (which has a main method and manifest file) is located in C:\Jar Folder. I tried to run it from cmd like this:
C:SomeRandomFolder> java -jar C:\Jar Folder\P2.jar
I get the error:
unable to access jarfile C:\Jar
It works when I run it from the folder that the jar is in like this:
C:\Jar Folder> java -jar P2.jar
Why can't I use the 1st command? Why do I have to go to the directory of my jar file?
That's because of the space you have in the path name. On Windows, use quotes:
java -jar "C:\Jar Folder\P2.jar"
If you are in a terminal and the folder you are in is deleted out from underneath you and replaced with the same content, the jar file will be be visible with ls, but will not be available since your current directory was deleted and replaced with one that looks just like it.
I got that error too:
el#apollo:/myproject/build/jar$ java -jar CalculateStats.jar
Unable to access jarfile CalculateStats.jar
To fix it. cd up a directory and cd back in, like this:
el#apollo:/myproject/build/jar$ cd ..
el#apollo:/myproject/build$ cd jar/
el#apollo:/myproject/build/jar$ java -jar CalculateStats.jar
Program completed successfully

Categories