Setting classpath from bat file doesnt work [duplicate] - java

This question already has answers here:
Defining and using a variable in batch file
(4 answers)
Closed 4 years ago.
So when i manually start my bat file that is on desktop, it works because executing a bat file via its icon uses directory of the icon(file) which is "C:\Users\Michael\Desktop".
set CLASSPATH = %~dp0 &:: %~dp0 stands for the directory of the bat file
java InitArray 5 0 4 &:: comment
pause
When i run the bat file above(InitArray.bat) from its desktop icon, it works.
java InitArray 5 0 4 &:: comment
pause
When i run the bat file above(test.bat) from its desktop icon, it works.
But when i run InitArray.bat from task scheduler, it uses the directory "C:\WINDOWS\system32". But that shouldnt be a problem because the first thing the bat file does is "set CLASSPATH = C:\Users\Michael\Desktop\".
Here is the result of task scheduler trying to run InitArray.bat.
C:\WINDOWS\system32>set CLASSPATH = C:\Users\Michael\Desktop\
C:\WINDOWS\system32>java InitArray 5 0 4
Error: Could not find or load main class InitArray
C:\WINDOWS\system32>pause
Press any key to continue . . .
Now, i know i can fix this issue by adding "C:\Users\Desktop\" to environment variable CLASSPATH. But i shouldnt need to do that since i am manually setting classpath to desktop in the first line of my bat file before trying to run the java class.

When setting a variable, everthing from the beginning of the variable name, until the last typed character is used as part of the variable name, before the = and value after the =. So:
set CLASSPATH = Somepath
Will end up with a variable name %CLASSPATH % (note the trailing space) and value Somepath (note the starting space.
Even if you add an accidental space after the value, it will become part of it, So this
set CLASSPATH=Somepath will end up with value with a trailing space Somepath
So always leave no space before or after the = and always enclose your code in double quotes to eliminate whitespace:
set "CLASSPATH=Somepath"
or in your actual case, it should look like:
set "CLASSPATH=%~dp0"

Set the CLASSPATH along with the java execution command or change directory using cd command.
cd /path_to_required_folder // set current directory
java -cp /classpath_location/ test.jar your.package.MainClass [args...]
Hope this helps.

Related

'lli' is not recognized as an internal or external command [duplicate]

Whenever I try and run mycommand.exe from my Windows cmd.exe terminal, I get this error:
''mycommand.exe' is not recognized as an internal or external command, operable program or batch file'
Secondly
I've also experienced a similar error when I tried to run C:\Program Files\My-App\Mobile.exe
''C:\Program' is not recognized as an internal or external command, operable program or batch file'
This is a very common question seen on Stackoverflow.
The important part here is not the command displayed in the error, but what the actual error tells you instead.
a Quick breakdown on why this error is received.
cmd.exe Being a terminal window relies on input and system Environment variables, in order to perform what you request it to do. it does NOT know the location of everything and it also does not know when to distinguish between commands or executable names which are separated by whitespace like space and tab or commands with whitespace as switch variables.
How do I fix this:
When Actual Command/executable fails
First we make sure, is the executable actually installed? If yes, continue with the rest, if not, install it first.
If you have any executable which you are attempting to run from cmd.exe then you need to tell cmd.exe where this file is located. There are 2 ways of doing this.
specify the full path to the file.
"C:\My_Files\mycommand.exe"
Add the location of the file to your environment Variables.
Goto:
------> Control Panel-> System-> Advanced System Settings->Environment Variables
In the System Variables Window, locate path and select edit
Now simply add your path to the end of the string, seperated by a semicolon ; as:
;C:\My_Files\
Save the changes and exit. You need to make sure that ANY cmd.exe windows you had open are then closed and re-opened to allow it to re-import the environment variables.
Now you should be able to run mycommand.exe from any path, within cmd.exe as the environment is aware of the path to it.
When C:\Program or Similar fails
This is a very simple error. Each string after a white space is seen as a different command in cmd.exe terminal, you simply have to enclose the entire path in double quotes in order for cmd.exe to see it as a single string, and not separate commands.
So to execute C:\Program Files\My-App\Mobile.exe simply run as:
"C:\Program Files\My-App\Mobile.exe"
When you want to run an executable file from the Command prompt, (cmd.exe), or a batch file, it will:
Search the current working directory for the executable file.
Search all locations specified in the %PATH% environment variable for the executable file.
If the file isn't found in either of those options you will need to either:
Specify the location of your executable.
Change the working directory to that which holds the executable.
Add the location to %PATH% by apending it, (recommended only with extreme caution).
You can see which locations are specified in %PATH% from the Command prompt, Echo %Path%.
Because of your reported error we can assume that Mobile.exe is not in the current directory or in a location specified within the %Path% variable, so you need to use 1., 2. or 3..
Examples for 1.
C:\directory_path_without_spaces\My-App\Mobile.exe
or:
"C:\directory path with spaces\My-App\Mobile.exe"
Alternatively you may try:
Start C:\directory_path_without_spaces\My-App\Mobile.exe
or
Start "" "C:\directory path with spaces\My-App\Mobile.exe"
Where "" is an empty title, (you can optionally add a string between those doublequotes).
Examples for 2.
CD /D C:\directory_path_without_spaces\My-App
Mobile.exe
or
CD /D "C:\directory path with spaces\My-App"
Mobile.exe
You could also use the /D option with Start to change the working directory for the executable to be run by the start command
Start /D C:\directory_path_without_spaces\My-App Mobile.exe
or
Start "" /D "C:\directory path with spaces\My-App" Mobile.exe
when you have this problem
search for Environment variables
press on Environment variables
Under "System varibles" you search for PATH and press edit
press on new and add the path of your program and save it

Running java programs from the command line on Windows 10 [duplicate]

This question already has answers here:
javac is not recognized as an internal or external command, operable program or batch file [closed]
(6 answers)
Closed 6 years ago.
I'm trying to run a basic "Hello World" app from the command line on windows 10 using
javac MyFirstProgram.java
but i receive
'javac' is not recognised as an internal or external command, operable program or batch
naturally, the first thing i did was google the problem and many solutions were presented, tried a few but nothing worked, has anybody else experienced this?
Steps to fix this error in windows 10/8/7
1.Check your javac path on Windows using Windows Explorer C:\Program Files\Java\jdk1.7.0_02\bin and copy the address.
2.Go to Control Panel. Environment Variables and Insert the address at the beginning of var. Path followed by semicolon. i.e C:\Program Files\Java\jdk1.7.0_02\bin; . Do not delete the path existent, just click in and go to the left end and paste the line above. Do not try anything else, because you just need to link your code to "javac.exe" and you just need to locate it.
3.Close your command prompt and reopen it,and write the code for compile and execution.
You need to add the location of your JDK to your PATH variable, if you wish to call javac.exe without the path.
set PATH=%PATH%;C:\path\to\your\JDK\bin\dir
Then...
javac.exe MyFirstProgram.java
OR, you can simply call it via the full path to javac.exe from your JDK installation e.g.
C:\path\to\your\JDK\bin\javac.exe MyFirstProgram.java

How can I pass java -jar arguments in a batch file?

How can I pass java -jar arguments in a batch file ?
Code below doesn't work:
set classpath = c:/users/abc/desktop/project/trunk/.CLASSPATH
java -jar java_file.jar "%classpath%"
( the java jar file needs to take the location of the .CLASSPATH file as the argument ) .
In my main batch file, classpath variable takes the value depending on which project is checked out from the SVN .
Hope my question is clear.
That's because your set command has spaces around the =. Remove them and it works.
What you have there now essentially means that an environment variable classpath<SPACE> is set to the given value which starts with a space. Therefore, the environment variable %classpath% (without the space in the variable name) doesn't exist at all.

how to set class path for more than one jar is used in command prompt in java [duplicate]

This question already has answers here:
Adding multiple jars to classpath on commandline [duplicate]
(2 answers)
Closed 8 years ago.
i am developing a program,for that want to use more than one jar(Example servlet.jar and mysql.jar) i know how to import them in using Eclipse IDE. but not in cmd prompt. Can anybody help me for this thanks in advance...
For multiple jars you can do
java -cp "path1/servlet.jar;path2/mysql.jar" yourPackage.Class
Or put all jars in one folder (let it be path1/lib) and do something like following
java -cp "path1/lib/*" yourPackage.Class
Note : classpath wildcards can be used only in Java 6 or later.
If you want to create a new classpath, write this:
set CLASSPATH=classpath1;
You can see all your classpaths if you type only this:
set
ClassPath set through command prompt will work only for current cmd window. Once you close it and open a new cmd window it will not work. Rather than setting classpath from command prompt, keep related paths to system properties:
For windows:
go to My Computer--> Properties--> Advance System Settings--> Environment Variables--> CLASSPATH--> put your path like this--> path1;path2;path3;. Don't forget to keep . (DOT) at the end.
you can set your class path using cmd prompt like :-
set classpath=(dir of classpath1);(dir of classpath2);(dir of class
path3);%CLASSPATH%
In enviorment variables , make a new variable named classpath and in value field write as much path you want but seperate them by semicolon.

How to call for the system console in Java?

I made a console-based Java application but every time I try to start the .jar file by clicking on it the program seems to be running but there is no console displayed. Is there specific code I must write in order to call for the system console?
Can you start the console first , change directory to where your jar file is and then run java -jar yiurjarfilename ?
The OS takes care of displaying console output. There is no code that you can write within Java to display or hide the "console" (because within Java, there's only standard output & error streams that you write to).
Windows usually leaves the console open after your program exits, but there might be a setting within the Java Runtime Environment that configures that behavior.
gcivil is right, you can see the results in console only if you start from console, if you are in windows you can open the command line Super + R and type cmd, then press enter (Super is the one with the Windows icon)
there you can type : java -jar "absolute path to your file" (don't forget the quotes)
another way is create a .bat file next to the .jar one, the bat file should contain
java -jar filename.jar
you don't need the quotes nor absolute path since it is next to the .bat file now you can double click that instead of the .jar
Once the app is terminated it will close the console, if you need to see what is next you have to add pause
java -jar filename.jar
pause

Categories

Resources