<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Arpan Banerjee</title>
    <description>The latest articles on DEV Community by Arpan Banerjee (@arpan_banerjee7).</description>
    <link>https://dev.to/arpan_banerjee7</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F401781%2F7ebd650d-df20-45ac-83d6-96c3550d47d7.jpeg</url>
      <title>DEV Community: Arpan Banerjee</title>
      <link>https://dev.to/arpan_banerjee7</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/arpan_banerjee7"/>
    <language>en</language>
    <item>
      <title>Run React Frontend and SpringBoot Backend on the same port and Package them as a single artifact !!</title>
      <dc:creator>Arpan Banerjee</dc:creator>
      <pubDate>Sat, 10 Apr 2021 19:02:01 +0000</pubDate>
      <link>https://dev.to/arpan_banerjee7/run-react-frontend-and-springboot-backend-on-the-same-port-and-package-them-as-a-single-artifact-14pa</link>
      <guid>https://dev.to/arpan_banerjee7/run-react-frontend-and-springboot-backend-on-the-same-port-and-package-them-as-a-single-artifact-14pa</guid>
      <description>&lt;h3&gt;
  
  
  Find out how to combine &lt;a href="https://reactjs.org/docs/create-a-new-react-app.html" rel="noopener noreferrer"&gt;create-react-app&lt;/a&gt; with Spring Boot to give you a single artifact.
&lt;/h3&gt;

&lt;p&gt;Hey there! I am a full stack developer with 3+ years of experience based out of West Bengal, India. &lt;em&gt;Today I will walk you through a setup trick to prepare your project structure for running react and spring boot app on the same port, and package them as a single artifact.&lt;/em&gt; You will see how easy this can make development when dealing with both of these game-changing technologies. &lt;/p&gt;

&lt;h4&gt;
  
  
  Here is the &lt;a href="https://github.com/arpan-banerjee7/demo-spring-react-maven-plugin" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt; link of the demo that I am going to explain here.
&lt;/h4&gt;

&lt;h1&gt;
  
  
  Advantages of this setup
&lt;/h1&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Combines the ease of development of both &lt;a href="https://reactjs.org/docs/create-a-new-react-app.html" rel="noopener noreferrer"&gt;create-react-app&lt;/a&gt; and Spring Boot. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You can use the hot reload feature of &lt;a href="https://reactjs.org/docs/create-a-new-react-app.html" rel="noopener noreferrer"&gt;create-react-app&lt;/a&gt; while developing the frontend, save the page after making changes and it will reflect immediately on the browser.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Using Spring Boot you can easily manage your development versions (using SNAPSHOTs) and release versions in production. You can detect build version and time at runtime in Spring Boot using  &lt;a href="https://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-features.html" rel="noopener noreferrer"&gt;Spring Actuator&lt;/a&gt; or Spring Admin, which can provide the build metadata functionality plus a lot more.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Package Frontend and Backend in a single war/jar file with optimized production build.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;I learned this in one of my recent projects. It has several spring boot microservices with react frontend packaged and deployed as single artifacts of their own.&lt;/p&gt;

&lt;p&gt;So, here is a quick guide how to run a react frontend and a spring boot backend on the same port and how to package them as a single jar file.&lt;/p&gt;

&lt;p&gt;First, &lt;strong&gt;create a spring boot project&lt;/strong&gt; with &lt;a href="https://start.spring.io" rel="noopener noreferrer"&gt;https://start.spring.io&lt;/a&gt;. Add the Web dependency. Set the groupId and artifactId to whatever you want. Generate the project and unzip it into your project directory.&lt;/p&gt;

&lt;p&gt;Or, if you are using Spring Tools Suite you can simply click &lt;br&gt;
&lt;code&gt;File-&amp;gt;New-&amp;gt;Spring Starter Project&lt;/code&gt; and mention the required details to create a spring boot project.&lt;/p&gt;

&lt;p&gt;Your initial pom.xml should look like this.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhopao37ru0jqi3ked9l5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhopao37ru0jqi3ked9l5.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The project structure should look like this.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F32we80nbgr4bhp20y62t.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F32we80nbgr4bhp20y62t.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can run the app now and see if it runs without any errors.&lt;br&gt;
Though you won't see anything if you hit &lt;a href="http://localhost:8080/" rel="noopener noreferrer"&gt;http://localhost:8080/&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuwacbl7mgf9rcdmf5e8t.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuwacbl7mgf9rcdmf5e8t.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, go to &lt;code&gt;src/main/resources&lt;/code&gt;, create a &lt;code&gt;static&lt;/code&gt; folder. Inside it create a &lt;code&gt;test.html&lt;/code&gt; file with some text in it.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fys0i7ck74hc89a72yck3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fys0i7ck74hc89a72yck3.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, if you restart the application and hit &lt;a href="http://localhost:8080/test.html" rel="noopener noreferrer"&gt;http://localhost:8080/test.html&lt;/a&gt; you will see it spins up your html page in &lt;em&gt;port 8080&lt;/em&gt;. You can see the contents of your file in the browser.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fo2oioz774lu3u1a7ch1n.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fo2oioz774lu3u1a7ch1n.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Serving the React App from Spring Boot on port 8080 and Packaging them together
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;We will exploit the above mentioned feature of Spring Boot to serve the single page of the react project.&lt;/em&gt; We will serve a html page from the &lt;code&gt;static&lt;/code&gt; folder in the &lt;code&gt;target&lt;/code&gt; directory, not in the source directory.&lt;/p&gt;

&lt;p&gt;Now let's create a react app, using &lt;a href="https://reactjs.org/docs/create-a-new-react-app.html" rel="noopener noreferrer"&gt;create-react-app&lt;/a&gt;. Navigate to your folder till &lt;code&gt;src/main&lt;/code&gt; in a terminal&lt;br&gt;
&lt;code&gt;D:\Codes_projects\springboot_react_maven_plugin\Spring_React_maven-plugin\src\main&amp;gt;&lt;/code&gt; and run &lt;code&gt;npx create-react-app frontend&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This should create a react app inside &lt;code&gt;src/main&lt;/code&gt;. Now if you look at your folder structure it should look like this.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdhix2exyhqbqp5r5tyf0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdhix2exyhqbqp5r5tyf0.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can run the react app, by doing &lt;code&gt;cd frontend&lt;/code&gt; and then &lt;code&gt;yarn start&lt;/code&gt; or &lt;code&gt;npm start&lt;/code&gt;. It should spin up the react app on &lt;a href="http://localhost:3000" rel="noopener noreferrer"&gt;http://localhost:3000&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsavbcfnjqro9ndhtsfwu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsavbcfnjqro9ndhtsfwu.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can create a production build of the frontend by running &lt;code&gt;yarn build&lt;/code&gt;. Your folder structure after running the build command will look like this.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fce90pom02jdx1u7865nj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fce90pom02jdx1u7865nj.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You need to copy this production build place it inside the &lt;code&gt;target/classes&lt;/code&gt; directory of your project to enable Spring Boot to serve this &lt;code&gt;index.html&lt;/code&gt; when you start your project&lt;/p&gt;

&lt;p&gt;So, there are two steps-&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;create a production build of the frontend&lt;/li&gt;
&lt;li&gt;copy the production build into ${target/classes/}&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Of course you are not going to do that manually.&lt;br&gt;
We we will use two &lt;code&gt;maven plugins&lt;/code&gt; for that.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;a href="https://github.com/eirslett/frontend-maven-plugin" rel="noopener noreferrer"&gt;frontend-maven-plugin&lt;/a&gt; for step 1.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://maven.apache.org/plugins/maven-resources-plugin/" rel="noopener noreferrer"&gt;maven-resources-plugin&lt;/a&gt; for step 2.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Add the following to your &lt;code&gt;pom.xml&lt;/code&gt; under the &lt;code&gt;plugins&lt;/code&gt; section and update the &lt;code&gt;properties section&lt;/code&gt; as shown.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

   &amp;lt;properties&amp;gt;
        &amp;lt;java.version&amp;gt;1.8&amp;lt;/java.version&amp;gt;
        &amp;lt;frontend-src-dir&amp;gt;${project.basedir}/src/main/frontend&amp;lt;/frontend-src-dir&amp;gt;
        &amp;lt;node.version&amp;gt;v14.15.4&amp;lt;/node.version&amp;gt;
        &amp;lt;yarn.version&amp;gt;v1.16.0&amp;lt;/yarn.version&amp;gt;
        &amp;lt;frontend-maven-plugin.version&amp;gt;1.7.6&amp;lt;/frontend-maven-plugin.version&amp;gt;
    &amp;lt;/properties&amp;gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

           &amp;lt;plugin&amp;gt;
                &amp;lt;groupId&amp;gt;com.github.eirslett&amp;lt;/groupId&amp;gt;
                &amp;lt;artifactId&amp;gt;frontend-maven-plugin&amp;lt;/artifactId&amp;gt;
                &amp;lt;version&amp;gt;${frontend-maven-plugin.version}&amp;lt;/version&amp;gt;

                &amp;lt;configuration&amp;gt;
                    &amp;lt;nodeVersion&amp;gt;${node.version}&amp;lt;/nodeVersion&amp;gt;
                    &amp;lt;yarnVersion&amp;gt;${yarn.version}&amp;lt;/yarnVersion&amp;gt;
                    &amp;lt;workingDirectory&amp;gt;${frontend-src-dir}&amp;lt;/workingDirectory&amp;gt;
                    &amp;lt;installDirectory&amp;gt;${project.build.directory}&amp;lt;/installDirectory&amp;gt;
                &amp;lt;/configuration&amp;gt;

                &amp;lt;executions&amp;gt;
                    &amp;lt;execution&amp;gt;
                        &amp;lt;id&amp;gt;install-frontend-tools&amp;lt;/id&amp;gt;
                        &amp;lt;goals&amp;gt;
                            &amp;lt;goal&amp;gt;install-node-and-yarn&amp;lt;/goal&amp;gt;
                        &amp;lt;/goals&amp;gt;
                    &amp;lt;/execution&amp;gt;

                    &amp;lt;execution&amp;gt;
                        &amp;lt;id&amp;gt;yarn-install&amp;lt;/id&amp;gt;
                        &amp;lt;goals&amp;gt;
                            &amp;lt;goal&amp;gt;yarn&amp;lt;/goal&amp;gt;
                        &amp;lt;/goals&amp;gt;
                        &amp;lt;configuration&amp;gt;
                            &amp;lt;arguments&amp;gt;install&amp;lt;/arguments&amp;gt;
                        &amp;lt;/configuration&amp;gt;
                    &amp;lt;/execution&amp;gt;

                    &amp;lt;execution&amp;gt;
                        &amp;lt;id&amp;gt;build-frontend&amp;lt;/id&amp;gt;
                        &amp;lt;goals&amp;gt;
                            &amp;lt;goal&amp;gt;yarn&amp;lt;/goal&amp;gt;
                        &amp;lt;/goals&amp;gt;
                        &amp;lt;phase&amp;gt;prepare-package&amp;lt;/phase&amp;gt;
                        &amp;lt;configuration&amp;gt;
                            &amp;lt;arguments&amp;gt;build&amp;lt;/arguments&amp;gt;
                        &amp;lt;/configuration&amp;gt;
                    &amp;lt;/execution&amp;gt;
                &amp;lt;/executions&amp;gt;
            &amp;lt;/plugin&amp;gt;   


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Now if you run &lt;code&gt;mvn clean install&lt;/code&gt; from &lt;code&gt;&lt;br&gt;
D:\Codes_projects\springboot_react_maven_plugin\Spring_React_maven-plugin&amp;gt;&lt;/code&gt;  maven will install npm, yarn and node locally and run npm build in the frontend directory.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fg74wfxixpajctpriqgib.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fg74wfxixpajctpriqgib.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The folder structure would look like this-&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F45meqmlcbpphpxvmdtfb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F45meqmlcbpphpxvmdtfb.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, add the second plugin to your &lt;code&gt;pom.xml&lt;/code&gt;.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

          &amp;lt;plugin&amp;gt;
                &amp;lt;artifactId&amp;gt;maven-resources-plugin&amp;lt;/artifactId&amp;gt;
                &amp;lt;executions&amp;gt;
                    &amp;lt;execution&amp;gt;
                        &amp;lt;id&amp;gt;position-react-build&amp;lt;/id&amp;gt;
                        &amp;lt;goals&amp;gt;
                            &amp;lt;goal&amp;gt;copy-resources&amp;lt;/goal&amp;gt;
                        &amp;lt;/goals&amp;gt;
                        &amp;lt;phase&amp;gt;prepare-package&amp;lt;/phase&amp;gt;
                        &amp;lt;configuration&amp;gt;
                            &amp;lt;outputDirectory&amp;gt;${project.build.outputDirectory}/static&amp;lt;/outputDirectory&amp;gt;
                            &amp;lt;resources&amp;gt;
                                &amp;lt;resource&amp;gt;
                                    &amp;lt;directory&amp;gt;${frontend-src-dir}/build&amp;lt;/directory&amp;gt;
                                    &amp;lt;filtering&amp;gt;false&amp;lt;/filtering&amp;gt;
                                &amp;lt;/resource&amp;gt;
                            &amp;lt;/resources&amp;gt;
                        &amp;lt;/configuration&amp;gt;
                    &amp;lt;/execution&amp;gt;
                &amp;lt;/executions&amp;gt;
            &amp;lt;/plugin&amp;gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Add the following dependency in the dependencies section.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

       &amp;lt;dependency&amp;gt;
            &amp;lt;groupId&amp;gt;org.projectlombok&amp;lt;/groupId&amp;gt;
            &amp;lt;artifactId&amp;gt;lombok&amp;lt;/artifactId&amp;gt;
            &amp;lt;optional&amp;gt;true&amp;lt;/optional&amp;gt;
        &amp;lt;/dependency&amp;gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Modify the &lt;code&gt;spring-boot-maven-plugin&lt;/code&gt; to include a configuration section.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

          &amp;lt;plugin&amp;gt;
                &amp;lt;groupId&amp;gt;org.springframework.boot&amp;lt;/groupId&amp;gt;
                &amp;lt;artifactId&amp;gt;spring-boot-maven-plugin&amp;lt;/artifactId&amp;gt;
                &amp;lt;configuration&amp;gt;
                    &amp;lt;excludes&amp;gt;
                        &amp;lt;exclude&amp;gt;
                            &amp;lt;groupId&amp;gt;org.projectlombok&amp;lt;/groupId&amp;gt;
                            &amp;lt;artifactId&amp;gt;lombok&amp;lt;/artifactId&amp;gt;
                        &amp;lt;/exclude&amp;gt;
                    &amp;lt;/excludes&amp;gt;
                &amp;lt;/configuration&amp;gt;
            &amp;lt;/plugin&amp;gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This will ensure that the frontend build files are copied after they have been generated by &lt;code&gt;yarn build&lt;/code&gt;.&lt;br&gt;
Now, run &lt;code&gt;mvn clean install&lt;/code&gt; again and inspect the &lt;code&gt;target/classes/static&lt;/code&gt; directory from your file manager.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgpx5ob6z5ofg1798bm7b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgpx5ob6z5ofg1798bm7b.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It will have the frontend production build files in there.&lt;/p&gt;

&lt;p&gt;Your final &lt;code&gt;pom.xml&lt;/code&gt; will look like this.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

&amp;lt;?xml version="1.0" encoding="UTF-8"?&amp;gt;
&amp;lt;project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"&amp;gt;
    &amp;lt;modelVersion&amp;gt;4.0.0&amp;lt;/modelVersion&amp;gt;
    &amp;lt;parent&amp;gt;
        &amp;lt;groupId&amp;gt;org.springframework.boot&amp;lt;/groupId&amp;gt;
        &amp;lt;artifactId&amp;gt;spring-boot-starter-parent&amp;lt;/artifactId&amp;gt;
        &amp;lt;version&amp;gt;2.4.2&amp;lt;/version&amp;gt;
        &amp;lt;relativePath /&amp;gt; &amp;lt;!-- lookup parent from repository --&amp;gt;
    &amp;lt;/parent&amp;gt;
    &amp;lt;groupId&amp;gt;com.springreact&amp;lt;/groupId&amp;gt;
    &amp;lt;artifactId&amp;gt;demo&amp;lt;/artifactId&amp;gt;
    &amp;lt;version&amp;gt;0.0.1-SNAPSHOT&amp;lt;/version&amp;gt;
    &amp;lt;name&amp;gt;demo&amp;lt;/name&amp;gt;
    &amp;lt;description&amp;gt;Run React Frontend and SpringBoot Backend on the same port.&amp;lt;/description&amp;gt;
    &amp;lt;properties&amp;gt;
        &amp;lt;java.version&amp;gt;1.8&amp;lt;/java.version&amp;gt;
        &amp;lt;frontend-src-dir&amp;gt;${project.basedir}/src/main/frontend&amp;lt;/frontend-src-dir&amp;gt;
        &amp;lt;node.version&amp;gt;v14.15.4&amp;lt;/node.version&amp;gt;
        &amp;lt;yarn.version&amp;gt;v1.16.0&amp;lt;/yarn.version&amp;gt;
        &amp;lt;frontend-maven-plugin.version&amp;gt;1.7.6&amp;lt;/frontend-maven-plugin.version&amp;gt;
    &amp;lt;/properties&amp;gt;
    &amp;lt;dependencies&amp;gt;
        &amp;lt;dependency&amp;gt;
            &amp;lt;groupId&amp;gt;org.springframework.boot&amp;lt;/groupId&amp;gt;
            &amp;lt;artifactId&amp;gt;spring-boot-starter-web&amp;lt;/artifactId&amp;gt;
        &amp;lt;/dependency&amp;gt;
        &amp;lt;dependency&amp;gt;
            &amp;lt;groupId&amp;gt;org.projectlombok&amp;lt;/groupId&amp;gt;
            &amp;lt;artifactId&amp;gt;lombok&amp;lt;/artifactId&amp;gt;
            &amp;lt;optional&amp;gt;true&amp;lt;/optional&amp;gt;
        &amp;lt;/dependency&amp;gt;
        &amp;lt;dependency&amp;gt;
            &amp;lt;groupId&amp;gt;org.springframework.boot&amp;lt;/groupId&amp;gt;
            &amp;lt;artifactId&amp;gt;spring-boot-starter-test&amp;lt;/artifactId&amp;gt;
            &amp;lt;scope&amp;gt;test&amp;lt;/scope&amp;gt;
        &amp;lt;/dependency&amp;gt;
        &amp;lt;dependency&amp;gt;
            &amp;lt;groupId&amp;gt;org.springframework.boot&amp;lt;/groupId&amp;gt; 
            &amp;lt;artifactId&amp;gt;spring-boot-starter-thymeleaf&amp;lt;/artifactId&amp;gt;
        &amp;lt;/dependency&amp;gt;
    &amp;lt;/dependencies&amp;gt;

    &amp;lt;build&amp;gt;
        &amp;lt;plugins&amp;gt;
            &amp;lt;plugin&amp;gt;
                &amp;lt;groupId&amp;gt;org.springframework.boot&amp;lt;/groupId&amp;gt;
                &amp;lt;artifactId&amp;gt;spring-boot-maven-plugin&amp;lt;/artifactId&amp;gt;
                &amp;lt;configuration&amp;gt;
                    &amp;lt;excludes&amp;gt;
                        &amp;lt;exclude&amp;gt;
                            &amp;lt;groupId&amp;gt;org.projectlombok&amp;lt;/groupId&amp;gt;
                            &amp;lt;artifactId&amp;gt;lombok&amp;lt;/artifactId&amp;gt;
                        &amp;lt;/exclude&amp;gt;
                    &amp;lt;/excludes&amp;gt;
                &amp;lt;/configuration&amp;gt;
            &amp;lt;/plugin&amp;gt;
            &amp;lt;plugin&amp;gt;
                &amp;lt;groupId&amp;gt;com.github.eirslett&amp;lt;/groupId&amp;gt;
                &amp;lt;artifactId&amp;gt;frontend-maven-plugin&amp;lt;/artifactId&amp;gt;
                &amp;lt;version&amp;gt;${frontend-maven-plugin.version}&amp;lt;/version&amp;gt;

                &amp;lt;configuration&amp;gt;
                    &amp;lt;nodeVersion&amp;gt;${node.version}&amp;lt;/nodeVersion&amp;gt;
                    &amp;lt;yarnVersion&amp;gt;${yarn.version}&amp;lt;/yarnVersion&amp;gt;
                    &amp;lt;workingDirectory&amp;gt;${frontend-src-dir}&amp;lt;/workingDirectory&amp;gt;
                    &amp;lt;installDirectory&amp;gt;${project.build.directory}&amp;lt;/installDirectory&amp;gt;
                &amp;lt;/configuration&amp;gt;

                &amp;lt;executions&amp;gt;
                    &amp;lt;execution&amp;gt;
                        &amp;lt;id&amp;gt;install-frontend-tools&amp;lt;/id&amp;gt;
                        &amp;lt;goals&amp;gt;
                            &amp;lt;goal&amp;gt;install-node-and-yarn&amp;lt;/goal&amp;gt;
                        &amp;lt;/goals&amp;gt;
                    &amp;lt;/execution&amp;gt;

                    &amp;lt;execution&amp;gt;
                        &amp;lt;id&amp;gt;yarn-install&amp;lt;/id&amp;gt;
                        &amp;lt;goals&amp;gt;
                            &amp;lt;goal&amp;gt;yarn&amp;lt;/goal&amp;gt;
                        &amp;lt;/goals&amp;gt;
                        &amp;lt;configuration&amp;gt;
                            &amp;lt;arguments&amp;gt;install&amp;lt;/arguments&amp;gt;
                        &amp;lt;/configuration&amp;gt;
                    &amp;lt;/execution&amp;gt;

                    &amp;lt;execution&amp;gt;
                        &amp;lt;id&amp;gt;build-frontend&amp;lt;/id&amp;gt;
                        &amp;lt;goals&amp;gt;
                            &amp;lt;goal&amp;gt;yarn&amp;lt;/goal&amp;gt;
                        &amp;lt;/goals&amp;gt;
                        &amp;lt;phase&amp;gt;prepare-package&amp;lt;/phase&amp;gt;
                        &amp;lt;configuration&amp;gt;
                            &amp;lt;arguments&amp;gt;build&amp;lt;/arguments&amp;gt;
                        &amp;lt;/configuration&amp;gt;
                    &amp;lt;/execution&amp;gt;
                &amp;lt;/executions&amp;gt;
            &amp;lt;/plugin&amp;gt;
            &amp;lt;plugin&amp;gt;
                &amp;lt;artifactId&amp;gt;maven-resources-plugin&amp;lt;/artifactId&amp;gt;
                &amp;lt;executions&amp;gt;
                    &amp;lt;execution&amp;gt;
                        &amp;lt;id&amp;gt;position-react-build&amp;lt;/id&amp;gt;
                        &amp;lt;goals&amp;gt;
                            &amp;lt;goal&amp;gt;copy-resources&amp;lt;/goal&amp;gt;
                        &amp;lt;/goals&amp;gt;
                        &amp;lt;phase&amp;gt;prepare-package&amp;lt;/phase&amp;gt;
                        &amp;lt;configuration&amp;gt;
                            &amp;lt;outputDirectory&amp;gt;${project.build.outputDirectory}/static&amp;lt;/outputDirectory&amp;gt;
                            &amp;lt;resources&amp;gt;
                                &amp;lt;resource&amp;gt;
                                    &amp;lt;directory&amp;gt;${frontend-src-dir}/build&amp;lt;/directory&amp;gt;
                                    &amp;lt;filtering&amp;gt;false&amp;lt;/filtering&amp;gt;
                                &amp;lt;/resource&amp;gt;
                            &amp;lt;/resources&amp;gt;
                        &amp;lt;/configuration&amp;gt;
                    &amp;lt;/execution&amp;gt;
                &amp;lt;/executions&amp;gt;
            &amp;lt;/plugin&amp;gt;
        &amp;lt;/plugins&amp;gt;
    &amp;lt;/build&amp;gt;
&amp;lt;/project&amp;gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Now you are ready to go! Run the project and go to &lt;a href="http://localhost:8080/index.html" rel="noopener noreferrer"&gt;http://localhost:8080/index.html&lt;/a&gt; VOILA!! You have run your React app via Spring Boot. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fijr7cxm5255xg1any814.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fijr7cxm5255xg1any814.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Taking a step further
&lt;/h3&gt;

&lt;p&gt;You can add &lt;code&gt;rest controllers&lt;/code&gt; and expose an endpoint to be the starting point of the application. Like an &lt;code&gt;Index Controller&lt;/code&gt; , whenever the application loads, spring boot will spin up the &lt;code&gt;index.html&lt;/code&gt; file  having the static contents from the optimized build of the frontend project from the &lt;code&gt;target directory&lt;/code&gt;. So that you can simply visit &lt;a href="http://localhost:8080" rel="noopener noreferrer"&gt;http://localhost:8080&lt;/a&gt; and not hardcode the &lt;code&gt;index.html&lt;/code&gt; file name.&lt;/p&gt;

&lt;p&gt;Let's do that.&lt;br&gt;
Add a controller, in the following folder structure.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7ol3rop2t3ftu4444dx8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7ol3rop2t3ftu4444dx8.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

package com.springreact.demo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class IndexController {

    @GetMapping("")
    public ModelAndView home() {
        ModelAndView mav=new ModelAndView("index");
        return mav;
    }

}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Run your application and hit &lt;a href="http://localhost:8080/" rel="noopener noreferrer"&gt;http://localhost:8080/&lt;/a&gt;.&lt;br&gt;
Did you see anything? No, right!&lt;/p&gt;

&lt;p&gt;You have to add another dependency for this to work.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

        &amp;lt;dependency&amp;gt;
            &amp;lt;groupId&amp;gt;org.springframework.boot&amp;lt;/groupId&amp;gt;
            &amp;lt;artifactId&amp;gt;spring-boot-starter-thymeleaf&amp;lt;/artifactId&amp;gt;
        &amp;lt;/dependency&amp;gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Now, by default &lt;code&gt;thymleaf&lt;/code&gt; looks for our templates in &lt;code&gt;src/main/resources/templates&lt;/code&gt;. We can put our templates there and organize them in sub-directories and have no issues.&lt;/p&gt;

&lt;p&gt;So, let's try that. Add a html file inside &lt;code&gt;src/main/resources/templates&lt;/code&gt; named &lt;code&gt;index.html&lt;/code&gt; and put some content in it.&lt;/p&gt;

&lt;p&gt;Run the application and visit &lt;a href="http://localhost:8080" rel="noopener noreferrer"&gt;http://localhost:8080&lt;/a&gt;. You will see the &lt;code&gt;index.html&lt;/code&gt; from &lt;code&gt;src/main/resources/templates&lt;/code&gt; being served in &lt;em&gt;port 8080&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffvoflqlxj97fea60qvae.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffvoflqlxj97fea60qvae.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, to change this default behaviour and make &lt;code&gt;thymleaf&lt;/code&gt; serve static content from another location, you need to add an entry to the &lt;code&gt;application.properties&lt;/code&gt; file.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

spring.thymeleaf.prefix=file:///D:///Codes_projects//springboot_react_maven_plugin//Spring_React_maven-plugin//target//classes//static/


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Here, I have hardcoded the path.&lt;/p&gt;

&lt;p&gt;Finally, now if you &lt;code&gt;update&lt;/code&gt; your project and start it. You can see the react app being served directly on &lt;em&gt;port 8080&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpw9ncenszv8fkzhtd6pw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpw9ncenszv8fkzhtd6pw.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;You have learned how to use spring boot to run a react app. You can run both your frontend and backend on the same port. I have explained what you need to do to get the setup done. You can now create a meaningful application, and see how easy and quick it is to develop with this setup. You can now run the frontend application using React-scripts by running &lt;code&gt;cd frontend&lt;/code&gt;; &lt;code&gt;yarn start&lt;/code&gt;, and develop using the hot reload feature of  &lt;a href="https://reactjs.org/docs/create-a-new-react-app.html" rel="noopener noreferrer"&gt;create-react-app&lt;/a&gt; and have meaningful error messages, while being able to deploy the application to test and production environments as a single artifact. &lt;/p&gt;

&lt;p&gt;Thanks for reading! &lt;/p&gt;

&lt;p&gt;Please leave a comment if you find this helpful. Any feedback or suggestions are welcome.&lt;/p&gt;

</description>
      <category>react</category>
      <category>springboot</category>
      <category>java</category>
      <category>maven</category>
    </item>
    <item>
      <title>Entry Components Vs Declarations in Angular</title>
      <dc:creator>Arpan Banerjee</dc:creator>
      <pubDate>Fri, 19 Jun 2020 10:24:08 +0000</pubDate>
      <link>https://dev.to/arpan_banerjee7/entry-components-vs-declarations-in-angular-5d</link>
      <guid>https://dev.to/arpan_banerjee7/entry-components-vs-declarations-in-angular-5d</guid>
      <description>&lt;h3&gt;
  
  
  Breaking down the differences when you are dealing with Angular components
&lt;/h3&gt;

&lt;p&gt;Hi! I am a full stack developer based out of West Bengal, India. I love digging deep into the trickier parts of the technologies. Here is one from my vault. Hope this helps you.&lt;br&gt;
Without further ado, let us dive straight into it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;For this you need to understand how angular actually works behind the scenes when it comes to creating components.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Any component as well as directives and pipes you plan on working with, you need to add them to your &lt;code&gt;declarations array&lt;/code&gt; in &lt;code&gt;@NgModule&lt;/code&gt; of &lt;code&gt;app.module.ts&lt;/code&gt;(while working with multiple modules, we import the feature module in our &lt;code&gt;app.module.ts&lt;/code&gt; imports array, and that feature module has all the components in its &lt;code&gt;declarations array&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;The above mentioned step is important for angular to understand what's a component or which components and directives you have in your app because, &lt;strong&gt;it does not automatically scan all your files&lt;/strong&gt;. You'd need to tell it which components exist, after creating a new component.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Still this alone only makes angular aware of it so that it is able to create such a component when it finds it in one of two places--&lt;/em&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;The first place would be in your templates if in your templates angular finds a &lt;code&gt;selector of a component&lt;/code&gt;--&amp;gt; then it basically looks into the `declarations array for that particular component--&amp;gt;finds that there and then is able to create that component.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The other place where angular will look for this component is in your &lt;code&gt;routes in your   rout config&lt;/code&gt;--&amp;gt; when you point at a component there angular will also &lt;code&gt;check   that in the declarations array&lt;/code&gt; and--&amp;gt; if it finds that there it is able to create such a component and load it.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now one place that &lt;strong&gt;does not&lt;/strong&gt; work by default is when you want to create a component manually in code. Like when you want to create a &lt;code&gt;dynamic component&lt;/code&gt; with &lt;code&gt;component factory&lt;/code&gt; like an alert component maybe which only shows up when there is any error and &lt;strong&gt;you neither mention the selector of it in any template nor in the rout configuration&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;And now, here angular does not automatically reach out to the declarations array. It simply doesn't do that. You can complain about that. Still it's the case.&lt;/p&gt;

&lt;p&gt;You instead deliberately need to inform angular that in this case the alert component will need to be created at some place and that angular basically should be prepared for this.&lt;/p&gt;

&lt;p&gt;Generally angular will prepare itself for this creation when it finds a component in the &lt;code&gt;template&lt;/code&gt; or in a &lt;code&gt;rout configuration&lt;/code&gt;.&lt;br&gt;
But in our case as we haven't done either of the two things as mentioned above, angular won't prepare itself.&lt;/p&gt;

&lt;p&gt;Now to tell angular to be prepared for the creation of that component you need to add a special property to the object you pass to &lt;code&gt;@ngModule&lt;/code&gt; besides &lt;code&gt;declarations&lt;/code&gt; &lt;code&gt;import&lt;/code&gt; and so on. That property is &lt;code&gt;entry components&lt;/code&gt;, it is an array of component types, but only those components that will eventually be created without &lt;code&gt;routs&lt;/code&gt; or &lt;code&gt;selectors&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;But with the launch of Angular 9, there were changes made behind the scenes, and it does all these work for you, you do not need to mention the entry components manually anymore.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I hope this clears some doubt regarding the entry components and declarations and how angular application works when it comes to deal with your components. &lt;a href="https://stackoverflow.com/questions/43815995/what-is-the-difference-between-declarations-and-entrycomponents/61896107#61896107"&gt;Here is the link to my stackoverflow answer.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Credits--Learned these concepts from the Udemy course "Angular the Complete Guide" -by Maximilian Schwarzmüller .&lt;/p&gt;

&lt;p&gt;Thank you, for reading!&lt;/p&gt;

</description>
      <category>angular</category>
      <category>entrycomponents</category>
      <category>declarations</category>
    </item>
    <item>
      <title>FileInputStream vs FileReader in Java</title>
      <dc:creator>Arpan Banerjee</dc:creator>
      <pubDate>Thu, 11 Jun 2020 14:25:19 +0000</pubDate>
      <link>https://dev.to/arpan_banerjee7/fileinputstream-vs-filereader-in-java-3je4</link>
      <guid>https://dev.to/arpan_banerjee7/fileinputstream-vs-filereader-in-java-3je4</guid>
      <description>&lt;h1&gt;
  
  
  To understand this thoroughly you need to understand what is character stream and what is byte stream, so let's have a quick look at that.
&lt;/h1&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Byte Streams&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;A byte stream access the file byte by byte. Java programs use byte streams to perform input and output of 8-bit bytes. It is suitable for any kind of file, however not quite appropriate for text files. For example, if the file is using a unicode encoding and a character is represented with two bytes, the byte stream will treat these separately and you will need to do the conversion yourself. Byte oriented streams do not use any encoding scheme while Character oriented streams use character encoding scheme(UNICODE). All byte stream classes are descended from InputStream and OutputStream .&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Character Stream&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;A character stream will read a file character by character. Character Stream is a higher level concept than Byte Stream . A Character Stream is, effectively, a Byte Stream that has been wrapped with logic that allows it to output characters from a specific encoding . That means, a character stream needs to be given the file's encoding in order to work properly. Character stream can support all types of character sets ASCII, Unicode, UTF-8, UTF-16 etc. All character stream classes are descended from Reader and Writer.&lt;/p&gt;

&lt;p&gt;If you try to read from a &lt;em&gt;.txt&lt;/em&gt; file which has been written with Uni-8 encoding which is by default used in java,then reading the file with both Reader and InputStream classes will give the same output.As here each byte represent one character.&lt;/p&gt;

&lt;p&gt;I have created few methods which will help you understand the difference between these two terms--&lt;br&gt;
&lt;em&gt;FileInputStream reads byte by byte&lt;/em&gt; &lt;br&gt;
&lt;em&gt;FileReader reads char by char&lt;/em&gt;. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Let's dive deeper into these concepts with examples.&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Now that you have got an idea about these two streams lets look at the examples to understand how it works internally.
&lt;/h3&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Method to write a some data in a file using Unicode 16 encoding&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--8JK5CEkk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/z3b7crhiovlfwr12ukr1.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--8JK5CEkk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/z3b7crhiovlfwr12ukr1.PNG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;output.txt&lt;/strong&gt; file will contain &lt;em&gt;Hello World&lt;/em&gt; in it.&lt;/p&gt;

&lt;h3&gt;
  
  
  These are the 3 ways of reading from the file first using FileReader the default way, then using FileInputStream and then using InputStreamReader with Unicode-16 charset(encoding).
&lt;/h3&gt;

&lt;h5&gt;
  
  
  The comments in the methods are self explanatory please read them to get a clear understanding how it works.
&lt;/h5&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;FileReader&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--C5ARWH9J--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/bvxnwxjnvoetvxjtlorf.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--C5ARWH9J--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/bvxnwxjnvoetvxjtlorf.PNG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;output&lt;/strong&gt; will be &lt;em&gt;Output of FileInputStream reading byte by byte : þ&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;FileInputStream&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--3O402-h5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/tj6802on6pd5y9vt8bdm.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--3O402-h5--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/tj6802on6pd5y9vt8bdm.PNG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;output&lt;/strong&gt; will be &lt;em&gt;Output of FileInputStream reading byte by byte : þ&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;InputStreamReader&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Gy9kYmAZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/pzqijhguyxb2ehcstsr9.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Gy9kYmAZ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/pzqijhguyxb2ehcstsr9.PNG" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;output&lt;/strong&gt; will be &lt;em&gt;uni-16 ISR: H&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;So using &lt;em&gt;InputStreamReader&lt;/em&gt; we can read the file.&lt;/p&gt;

&lt;p&gt;Hope this clears up some doubt on how Java IO works.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://stackoverflow.com/questions/5155226/fileinputstream-vs-filereader/61307867#61307867"&gt;Here&lt;/a&gt; is the link to the &lt;strong&gt;stackoverflow&lt;/strong&gt; discussion , you can find my answer there as well.&lt;/p&gt;

&lt;p&gt;Thank you or reading :)&lt;/p&gt;

&lt;p&gt;If you have any question regarding this or anything I should add, correct or remove, feel free to comment, email or DM me. Thanks once again !!&lt;/p&gt;

</description>
      <category>java</category>
      <category>stream</category>
      <category>reader</category>
      <category>javaio</category>
    </item>
  </channel>
</rss>
