Can I get the path of the currently running java executable? - java

Suppose I want to run a java program from the command line and I use this command:
myExes\java\java.exe AJavaProgram
As you can see, java.exe is not in my path, so I am running it manually rather than simply using the command java AJavaProgram.
I would like the program to return/print the first entry in the command, in this case, that entry is myExes\java. (Including java.exe at the end of this is also fine).
Is there a way to do this?
Initially, I thought it would be simple. args[0] would return the path, but that is not the case.

ProcessHandle.current() returns the current Java process. You can use that to see the full command in the process handle’s info:
ProcessHandle.current().info().command().ifPresent(
cmd -> System.out.println(cmd));

You can't get the string "myExes\java\java.exe", but you can get the location of the Java installation.
The following are results for running with OpenJDK 14 on Windows 10:
System.out.println(System.getProperty("java.home"));
System.out.println(System.getProperty("sun.boot.library.path"));
Output
C:\prog\Java64\jdk-14
C:\prog\Java64\jdk-14\bin
For reference, the full path of java.exe is:
C:\prog\Java64\jdk-14\bin\java.exe

When you do myExes\java\java.exe AJavaProgram AJavaProgram is the arg to java.exe and not the reverse. Its the same when you do java AJavaProgram, AJavaProgram is the arg to java.

How about this way?
You can get a java home dir.
String path = System.getProperty("java.home");

Related

Java jdk path setting in windows

I delete jdk\bin path from environment variables path in windows but when ı write "java -version" in anywhere , it still returns true results . How ?
You can use the where command, to check which java application is executed when you run java -version
Is there an equivalent of 'which' on the Windows command line?
Just type where java in cmd console, and you'll get where it is. And as it shows, I think you need check this location(C:\Windows\System32), when you instal java, Java exe files are copied to windows System32 folder, java.exe, javaw.exe and javaws.exe are available there.
Restart your system and try again. In case, it's still pointing after restart, please check the path variable correctly. Might be some referrence still exist in either system/user path variable and thus giving the result.

Javac command in cmd

I have problems compiling Java programs from command line interfaces (both command prompt and NetBeans terminal).
I added the jdk directory to the PATH system variable but not all commands are recognizable, some commands (in the jdk1.6.0_37\bin folder) are recognized (like: java, javaw, packger)and others I'd have to type the full directory name for it to work (like: jar, javac, javah, javap) (this applies for both cmd and NB).
I don't know why this should be, all of these files are .exe , all are Java Platform SE binary, the only difference I can see is the icon of the files, the ones that work have the Java logo (Coffee Mug) and the rest have the standard .exe logo. Any help would be appreciated. Thanks
You have to add the jdk1.6.0_37\bin directory to the PATH variable. That is where all the commands that you want to run are in. After you add the directory to the PATH variable, open a new command prompt and run those commands again.
I'm searched many answers that suggest me to type in cmd:
set path = "%path%;c:program files\java\jdk1.7.0\bin"
but this is WRONG!
the right solution is that you leave "set" and just type
path = %path%;c:program files\java\jdk1.7.0\bin
P/s: of course you have to replace "jdk1.7.0" folder by your current java version folder

setting up classpath. javac is not recognized

I am trying to run my java program from command line.
I read an article about setting up classpath, but I get an error of javac is
not recognized as internal or external command. What should I do? (I dont want to set a permanent CLASSPATH)
This is what I have done in my command line
D:\user> set path=%path%;C:\Program Files\Java\1.7.0_07\bin
D:\user> cd testing
D:\user\testing> javac firstProgram.java
'javac' is not recognized as an internal or external command,
operable program or batch file.
Thank you
Assuming that the PATH is correct1, the most likely cause is that you have a JRE installation ... and a JRE doesn't include a java compiler. You need a JDK installation if you want to compile from the command line.
(You can confirm this by looking in the C:\Program Files\Java\1.7.0_07\bin directory to see if it contains a javac.exe file. A JRE won't ...)
Where can I find the Java compiler to download..
You need to download one of the JDK installers; see http://www.oracle.com/technetwork/java/javase/downloads/index.html
1 - I don't think quotes are required in a PATH variable on Windows. At least that's what various examples that Google found for me seem to imply. But I've never really understood the logic behind quoting in Windows ...
Its an issue related to Program Files.
First make sure that your JDK Folder is installed in Program Files or Program Files(x86) or any other folder.
Then you should use the path of bin folder in " ". Because command prompt does break the string at space. When you will write it in " " then it will take is as a whole String.
You try these commands
set path=%path%;"C:\Program Files\Java\1.7.0_07\bin"
or
set path=%path%;"C:\Program Files(x86)\Java\1.7.0_07\bin"
It might help you to get out of this.
Better do it in Environmental variable and check it!
try below command is recognized from command prompt
C:\Program Files\Java\1.7.0_07\bin\javac ab.java
This is just to verify your javac
Here's how you can set the path temporary, meaning if you close and reopen "command prompt" you will have to set the path again.
Assuming the path is C:\Program Files\Java\jdk1.6.0\bin
TYPE IN C:\Program Files\Java\jdk1.6.0\bin AND HIT ENTER
that's it.
The commands D:\user> set path=%path%;C:\Program Files\Java\1.7.0_07\bin works well for me
Adding few more information to this:
Please check the version of JDK and JRE installed on your computer. Recently I faced the same problem even after setting the PATH. It gives the error "javac - command is not recognised"
Solution is there must be similar versions of JDK as well as JRE
E.g.: JDK 1.7.75 along with JRE 1.7.75

Shell script: How to put absolute path into command?

I was trying to execute my java program through shell script so I wrote:
java -jar $(pwd)"/test.jar"
It worked flawlessly but when I turned to the code below:
PATH=$(pwd)"/test.jar"'
java -jar $PATH
Then I got an error: "Run.sh: 3: java: not found"
(Running on Ubuntu)
I have very little experience in shell script so please let me know what's wrong with it. Thanks.
PATH is a special environment variable which the shell uses to find executables. You've changed PATH to point at test.jar, so now the shell can't find java.
Call your variable something else.
Example:
LIB_PATH="$(pwd)/test.jar"
java -jar ${LIB_PATH}
The value in $(PWD) depends on the directory the script is called from (print working directory). If you call the script from another directory, than the one your jar-files resides in, you'll get the wrong path. And you changed the search path of the SHELL, that will prevent the shell from finding any other binary e.g. java.
PATH is system-reserved variable, that define the way where your system should look to find the executable (in your case java). Therefore you shouldn't use it in your code as variable to your test.jar .
In my opinion, your code should be something like:
#!/bin/sh
PROGPATH='/path/to/your/test.jar'
JAVAEXEC=`which java`
JAVAPARAMS='-j'
GLOBALPATH="$JAVAEXEC $JAVAPARAMS $PROGPATH"
echo $GLOBALPATH

How to Run a Simple Java Program

I dad left Java since so long as a result now it happens that sometimes I forget the simple things and used to behave like a Stupid.
To run a Simple Java program say "Hello World" written in Notepad what do I have to Do?
I know the commands javac "Filename.java" and java "Filename" respectively to run it from the Command prompt.
But When I try to do that I got this message:
"javac is not recognized as an internal or external command, operable program or batch file."
and I could not Complie the file.
I hava little idea that we need to do some stuffs like setting the classpath or perhaps the path evnironment variables but it was exactly that I don't remember.
Can anybody please help me?
Thanks,
david
Add a JAVA_HOME env variable to point to the jdk installation directory
To your PATH env variable, add %JAVA_HOME%\bin
Add a CLASSPATH env variable to point to %JAVA_HOME%\lib.
remember to open a new console window and try running javac and java - everything should be fine now.
1) create JAVA_HOME environmental variable set value to java home directory
e.g. c:\program files\java\jdk1.5;
1) set PATH in environmental variable to your java bin directory
e.g. %JAVA_HOME%\bin
and to check classpath is set correctly run javac command on cmd
and this link will help to create and run simple java application
java tutorial
this might be usefull budddy
http://www.apl.jhu.edu/~hall/java/beginner/settingup.html
You need the JDK to be able to run javac.
I suggest you first start coding in eclipse, it provides all the environment set up for you. Once you get good with coding, you can try command prompt compiling and running. That way, you will be confident with language first and then go into the nitty-gritties of the environment and set up.
Its better to use any java IDE either eclipse or netBeans Download Link
But in case if you like to go through Command prompt method, then u need to set the paths. (These are the variables for your OS, that used to know where your commands e.g. java or javac etc are located). Hope from other answers you set the paths.
Good luck

Categories