Skip to content

Commit

Permalink
Merge pull request #108 from JesseK123/main
Browse files Browse the repository at this point in the history
 Fix #961
  • Loading branch information
TracyRagan authored Oct 23, 2024
2 parents 2a0ebb8 + c80f3e1 commit 4d87f6f
Show file tree
Hide file tree
Showing 10 changed files with 102 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,69 +7,131 @@ description: >
---


## Steps for running java application in your local machine
# Steps for running java application in your local machine

**Before Starting**
## **Before Starting (Running Linux Virtual Environment)**
This use case involves running a java application locally on a Linux virtual environment hosted on a Windows 11 laptop.

- I have wsl2 installed in my system and I am using my Linux environment to run this application. I also advised developers to use
- Linux environment as we will be going to use lot of command line stuff later.
- You can also run it in eclipse or other code editor for demo purposes.
## **Tools needed:**

**Software needed :**
Java
Maven
VS Code
Linux in WSL2
- [Java17](https://www.oracle.com/java/technologies/downloads/#jdk23-windows)

**Sample Application**
- [Java Apache Maven](https://maven.apache.org/download.cgi)

Let’s clone the sample application that we’ll be using in this module to our local development machine. Run the following commands in a terminal to clone the repo.
- [VS Code](https://code.visualstudio.com/download)

- Linux Environment ([Ubuntu](https://learn.microsoft.com/en-us/windows/wsl/install))

## **Sample Application**

Let’s clone the sample application that we’ll be using in this module to our local development machine. Run the following commands in a windows Command Prompt to clone the [repository](https://github.com/spring-projects/spring-petclinic).

Note: The sample application used in this tutorial is Spring Pet Clinic application and can be exchanged with any other java application
```
cd /path/to/working_directory
git clone https://github.com/spring-projects/spring-petclinic.git
cd spring-petclinic
./mvnw package
java -jar target/*.jar
```
cd /path/to/working/directory
git clone https://github.com/spring-projects/spring-petclinic.git
cd spring-petclinic
## **Java and Maven version**

This is the Java and Java Maven used for this tutorial:
```
**Java and Maven version**
<DEVICE_NAME>:/$ mvn -version
```
![Maven Version](/Maven_Version.png)

Its the version of maven and java I am using.
```
<Device_Name>:/$ java -version
```
![Java Version](/Java_Version.png)

![java Tab](/java_maven_version.png)
## **Steps to run application locally**

**Steps to run application locally**
**1. Mount the relevant application folder, so it lies on the command line, as shown below**

1. Go to your directory where your application lies in command line. Like its my working directory.
```
<DEVICE_NAME>:/mnt<address of application folder>$
```

"/mnt" - command used to attach file systems onto Linux

```arvind@DESKTOP-1DEUAEO:/mnt/c/Users/arvin/Desktop/spring-petclinic$```
Then attach the address of the sample application stored

2. Run the application using ``
mvnw spring-boot:run
`` command or ``
mvn spring-boot:run
`` command.
It will takes some time to run application for first time. When I run this first time in my machine, it took almost 16 mins, so wait for a while.
e.g.

![java Tab](/application_started.png)
```
arvind@DESKTOP-1DEUAEO:/mnt/c/Users/arvin/Desktop/spring-petclinic$
```

**Note :** If while running this command ``mvnw spring-boot:run`` if it is showing error, you have to install mvnw using this command ``mvn -N wrapper:wrapper`` and then try to run the prvious command again.
In wsl2, mvnw is still not working sometimes due to windows issue, then run this -
This is where the sample application (Spring Pet Clinic) is stored in "c/Users/arvind/Desktop"

sudo apt install dos2unix
dos2unix mvnw
\
**2. Run the application**

Now your mvnw will works properly.
Using the Command:
```
<DEVICE_NAME>:/mnt/.../working folder/spring-petclinic$ mvn spring-boot:run
```



This allows Maven to trigger the execution of compile, test and package phases automatically, and the application is started.

It will take some time to run application for first time.

3. When you get this in your terminal, it means your application is started now. Now visit your `http://localhost:8080`.
4. It will look something like this in your browser.

![java Tab](/spring_ui.png)
![Application_Running](/Spring_Pet_Clinic.png)
Now your application will start to run.

\
**3. To find the IP address of the Ubuntu Virtual Environment hosting the application**
```
<DEVICE_NAME>:/$ hostname -l
```
e.g. xxx.xx.xx.xx

\
**4. Then type “http://<IP Address>:8080” into a browser in your Windows. The screen below should appear**

**Errors**
![Web-page](/Spring_Pet_Clinic_Result.png)

## **Errors**

Error you can get while running the application.

You will get something like this which says execution failed and list some java files.
![java Tab](/maven_error.png)
### *Error 1*

You will get something like this which says execution failed and list some java files

![incompatiblity](/Incompatability_Error.png)

In this case, open your application in Visual Studio Code:

**1. Select the relevant folder for the application:**
Do this by pressing CRTL + K and then CRTL + O

![Select Project](/Selecting_File.png)

**2. Then select the relevant files that has the error**

\
**3. To change the format of the line break, find the CRLF icon in the bottom right corner:**

![Finding CRLF](/Selecting_CRLF.png)

Then select LF, this is because Linux uses LF as the new line character format


![Choosing new line character](/ChangingLF.png)


### *Error 2*

Another error that may occur is the version of Java installed:

![Version Error](/Version_Error.png)

To do this, ensure that the [latest version of Java](https://www.oracle.com/java/technologies/downloads/) is installed

In this case, just open your application in vs code and convert it from CRLF to LF which you can find in the right bottom of the vscode.
Binary file added static/ChangingLF.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added static/Incompatability_Error.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added static/Java_Version.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added static/Maven_Version.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added static/Selecting_CRLF.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added static/Selecting_File.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added static/Spring_Pet_Clinic.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added static/Spring_Pet_Clinic_Result.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added static/Version_Error.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 4d87f6f

Please sign in to comment.