Bash on windows java

Open git bash and run command in java on Windows

I’m using java on Windows, and want to call a Linux command so I’m trying to open git bash and paste some commands. I’m able to open git bash but can’t paste anything.

This opens git bash fine:

When I do this, git bash opens but closes right away:

3 Answers 3

You just have to change the paths and the git command. But the git-bash output is printed on a separate .txt file because I couldn’t read it in any other way.

Do you need the bash terminal to pop up? If not, this could work

This will execute a bash script on Windows if you have Git installed without the need to write the output to a temp file.

The contents of the script:

Also, this will execute Git Bash silently in that you won’t get a popup window while processing.

Not the answer you’re looking for? Browse other questions tagged java bash curl or ask your own question.

Linked

Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

site design / logo © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2021.4.16.39093

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Running Bash commands in Java

I have the following class. It allows me to execute commands through java.

When I run commands, the result of the previous command isn’t saved. For example:

Gives the output:

Why doesn’t the second ‘ls’ command show the contents of the ‘bin’ directory?

7 Answers 7

You start a new process with Runtime.exec(command) . Each process has a working directory. This is normally the directory in which the parent process was started, but you can change the directory in which your process is started.

I would recommend to use ProcessBuilder

If you want to run multiple commands in a shell it would be better to create a temporary shell script and run this.

Of course you can also use any other script on your system. Generating a script at runtime makes sometimes sense, e.g. if the commands that are executed have to change. But you should first try to create one script that you can call with arguments instead of generating it dynamically at runtime.

It might also be reasonable to use a template engine like velocity if the script generation is complex.

EDIT

You should also consider to hide the complexity of the process builder behind a simple interface.

Separate what you need (the interface) from how it is done (the implementation).

Читайте также:  Windows 10 смена раскладки клавиатуры alt shift справа

You can then provide implementations that use the process builder or maybe native methods to do the job and you can provide different implementations for different environments like linux or windows.

Finally such an interface is also easier to mock in unit tests.

Windows Subsystem for Linux not recognizing JAVA_HOME Environmental Variable

I’m trying to get WSL to recognize my windows installed environmental variable of JAVA_HOME. I attached of what I have in my bashrc and what I have in my windows environmental variables along with outputs from cmd and bash.

What’s at the end of my bashrc:

I’ve been stuck on this for awhile and can’t figure it out or find a working solution online. Thanks!

3 Answers 3

As Biswapriyo suggested, you should use WSLENV.

Open PowerShell. Then set JAVA_HOME to the path to your java installation.

In your case, run setx JAVA_HOME «D:\Program Files\Java\jdk-11.0.1»

You should see a message that says «SUCCESS: Specified value was saved.

  1. Then run setx WSLENV «JAVA_HOME/p»

You should see the success message again.

  1. Type ‘env’ into your WSL bash prompt.

You should see JAVA_HOME correctly set at this point.

Note: If step 2 doesn’t work, you might want to changing the path to JAVA_HOME to include the ‘\bin’ folder.

Since I’ve never been able to share variables between the 2 systems easily, I creayed a simple bash function which can easily retrieve (and define, if asked to) any Windows Environment variable. It also takes care of paths so they get converted from Win32 to Un*x-line.

I added this to /etc/bash.bashrc :

And all I have to do to display one is to call winenv PROGRAMFILES (for example)
Or if I expect to export it, I just have to add a -d argument before the variable name as in winenv -d WINDIR .

TL;DR: In WSL, you must use javac.exe since it is a Windows binary. Simply typing javac will not work, even if the path is set up correctly. If that doesn’t work, try adding ../bin to the end of your JAVA_HOME variable.

Using Windows Binaries & Environment Variables in WSL

There’s a much easier way to make Windows and WSL utilize the same JavaSDK binary, you just need to set up a few things first. Best of all, if you have JavaSDK installed on Windows, you do not need to install Linux binaries.

In WSL, list symbolic links on PC:

If any drive is owned by root , perform your WSL dev work in /mnt/c/Users/

Personally, I create a development directory in Windows and add a symbolic link to the directory in WSL:

cd dev now brings you to your development directory.

Ensure Java for Windows works

Open PowerShell / cmd.exe from any directory and enter: java —version

You should get a list of JRE info:

Your version might be different, the important part is that the system knows where to find Java. If you get an error, ensure your Windows Environment variables are set correctly:

  • JAVA_HOME as an Environment Variable, and
  • JAVA_HOME/bin as a Path variable.

Setting Variable in WSL

The best place to put the next lines of code are in your .bashrc file, but if you have a ./bash_profile or /etc/profile structure set up, you can put it there.

Читайте также:  Установка кодека hevc для windows 10

While we’re at it, let’s add Maven too:

I have my WSL, Java, and all my other dev tools set up on my second HDD which is not a system drive, ensure that your location matches your JAVA_HOME path in Windows.

For instance, if in Windows, Java is located at: C:\Java\jdk8.0 The corresponding WSL mount point is: /mnt/c/Java/jdk8.0

Executing

Windows PowerShell/cmd:

WSL:

Simplicity

We don’t want to install a second copy of Java specific to WSL and waste that precious disk space, so we’re going to call the Windows binary from the WSL shell. This is a great benefit of WSL—WSL1 in particular—in that it can interact (almost) flawlessly with the Windows File System.

NOTE: In order to run a program, it must either be a Path variable, or be run from within it’s containing folder.

Hopefully that works as easily for you as it did for me. Just remember to use the correct command depending on what OS binary you’re running. This took me about 10 minutes to get set up, and has been a lifesaver for cross-compiling and general ease-of-use.

how do i set JAVA_HOME in bash?

my host system is windows 7

I am trying to get this to work in CYGWIN.

I need to set an environment variable called JAVA_HOME to be c/program files (x86)/java/jre7

here’s what i tried:

but i am getting:

line 9: c/program files (x86)/java/jre7: No such file or directory Error: JAVA_HOME is not set.

what am i doing wrong?

3 Answers 3

Two errors: First, you’re attempting to execute » c/program files (x86)/java/jre7 » with a wierdly set one-shot variable, second, it looks like you’re using bourne-shell semantics: first you must set variable, then you can export it.

The line $myjava=cat c/program\ files\ (x86)/java/jre7 says take the variable named «myjava», extract the value (wait, that var doesn’t exist? well, the value will be the empty string. ), use that value as the name of a variable, and set that variable to have the value «cat». Don’t actually set this in the shell environment, we’ll run «c/program\ files\ (x86)/java/jre7» and set this as an extra variable in that command’s environment. That’s why you’re getting «c/program files (x86)/java/jre7: No such file or directory».

The line export JAVA_HOME=myjava , in bash I would say that means create/set a variable named JAVA_HOME , set it to the string «myjava», and flag it as an exported variable. The error message «Error: JAVA_HOME is not set.» either means that you need to break it up (not using bash?), as JAVA_HOME=$myjava; export JAVA_HOME , or it means that JAVA_HOME is not set to anything that makes sense, and if you fix the first line, it’ll all get better.

@BMargulies Answer has provided a solution. But I think it is relevant to understand why what you did was wrong . so that next time you can take (at least) a better stab at getting it correct.

Looking at your first command line .

There are a number things wrong with that. First, the syntax for setting a shell variable is:

so what you would actually be doing in the first line is follows:

It is setting a variable with a some unknown variable name to the string «cat»:

  • The variable name is unknown because it depends on the prior value of the «myjava» variable. (The utterance «$myjava» says «interpolate the value of the ‘myjava’ variable here».)
  • The reason that it is setting the variable to «cat» is that «cat» is the first word after the «=».
Читайте также:  Mac os default wallpapers

Then, it is attempting to run the rest of the line (i.e. «c/program\ files\ (x86)/java/jre7») as a command. But it isn’t a command: it is a directory. And that’s the most likely source of the «Not Found» error message you are seeing.

On top of that, you seem to be trying to use the «cat» command for something. The «cat» command (in that form) reads the contents of a file and writes it to standard output.

  1. You don’t have a file. You are looking at a directory.
  2. You don’t care about the contents of the directory. (And that’s not how you would access it . )
  3. You are not looking at standard out.

In short, «cat» is completely unnecessary here. You just want the pathname for the directory. So the correct form of the first line would be:

or using quotation marks:

The three kinds of quoting all have the same effect. They cause the stuff after the «=» to be parsed as a single . (There are differences in behaviour between the 3 quoting styles, but they don’t matter here.)

The second line is actually close to what it needs to be. You wrote:

means set JAVA_HOME to the literal string «myjava» . and export it. But you want to use the value of the shell variable named «myjava». That is:

The «$myjava» says «interpolate the value of the variable ‘myjava'» . see above.

@BMargulies answer combines the two lines into on, and does away with the (unnecessary) local variable «myjava». That is the way I’d have done it too.

How do you set java version in windows git bash?

How do you set the java version to use in a windows git bash? Does it work via the environment variables? Java -version gives another version in dos command prompt than in git bash.

5 Answers 5

You don’t set the java version. You are seeking the $PATH (bash) and the %PATH% (Windows/DOS/cmd.exe) environment variables. When you run the command java -version , the shell searches the directories on the $PATH to locate the java program and run it with the given arguments. If you get different results in different shells then that means you have different PATHs such that a different installation of Java is found.

Let’s say you want to try other Java versions but you don’t want to alter the Windows environment variables.

    Go to your home folder. i.e. C:\Users\MyAccountName

Edit the .bashrc file and add the following code below.

.bashrc and .bash_profile are files created during the git-bash installation.

Yes the environment variables affect which java is used in Git Bash. But if setting the $JAVA_HOME and $PATH variables doesn’t seem work, try this:

  1. Go to the folder where Git Bash is installed (usually C:\Program Files\Git)
  2. Go to usr/bin folder, i.e. C:\Program Files\Git\usr\bin
  3. There should be a file named «java» there. Rename it to «notjava».

Now set the PATH variable as described in the other answers. I.e.

Restart Git Bash.

This is what worked for me. I’m using Git for Windows v2.17.0.

Оцените статью