User Tools

Site Tools


informatica:linux:java:maven

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
informatica:linux:java:maven [2015/04/13 20:19] – external edit 127.0.0.1informatica:linux:java:maven [2017/07/26 11:49] (current) – [Proyecto POST] jose
Line 29: Line 29:
 Aplicación sola: \\ Aplicación sola: \\
 {{:informatica:linux:java:miappweb.war|}} {{:informatica:linux:java:miappweb.war|}}
 +
 +====== Proyecto Hello World ======
 +Fuente: https://cwiki.apache.org/confluence/display/MAVEN/Tutorial%3A+Build+a+JAR+file+with+Maven+in+5+minutes
 +
 +  group id: org.iwanttobefreak.post
 +  artifact id: java-archive
 +  version: 1.0-SNAPSHOT
 +Creamos el siguiente pom.xml
 +<code>
 +<project>
 + <modelVersion>4.0.0</modelVersion>
 + <groupId>org.iwanttobefreak.post</groupId>
 + <artifactId>java-archive</artifactId>
 + <version>1.0-SNAPSHOT</version>
 +</project>
 +</code>
 +
 +También podemos añadir la versión de java, me daba algún error porque era la 5 por defecto:
 +<code>
 +<properties>
 +    <maven.compiler.source>1.7</maven.compiler.source>
 +    <maven.compiler.target>1.7</maven.compiler.target>
 +</properties>
 +</code>
 +
 +Ahora creamos el código java en esta ruta:
 +  mkdir -p src/main/java/org/iwanttobefreak/post/
 +
 +
 +El nombre del fichero .java, en este caso Main es la clase que declaramos en el código fuente
 +  src/main/java/org/iwanttobefreak/post/Main.java
 +<code>
 +package org.iwanttobefreak.post;
 +public class Main {
 +  public static void main(String[] args) {
 +    System.out.println("Hello world");
 +  }
 +}
 +</code>
 +
 +Compilamos el paquete:
 +  mvn package
 +Nos crea el jar en el directorio target.
 +
 +Lo ejecutamos:
 +  java -cp ./target/java-archive-1.0-SNAPSHOT.jar org.iwanttobefreak.post.Main
 +  Hello world
 +
 +====== Proyecto WGET ======
 +<code>
 +package org.iwanttobefreak.post;
 +
 +import java.io.BufferedReader;
 +import java.io.InputStreamReader;
 +import java.net.URL;
 +
 +public class Main {
 +        public static void main(String[] args) {
 +                try {
 +                        URL url = new URL("http://www.google.es/");
 +                        BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
 +                        String strTemp = "";
 +                        while (null != (strTemp = br.readLine())) {
 +                                System.out.println(strTemp);
 +                        }
 +                } catch (Exception ex) {
 +                        ex.printStackTrace();
 +                }
 +        }
 +}
 +</code>
 +
 +
 +====== Proyecto POST ======
 +
 +<code>
 +package com.iwanttobefreak.post;
 +
 +import java.io.BufferedReader;
 +import java.io.DataOutputStream;
 +import java.io.InputStreamReader;
 +import java.net.HttpURLConnection;
 +import java.net.URL;
 +
 +import javax.net.ssl.HttpsURLConnection;
 +
 +public class HttpURLConnectionExample {
 +
 + private final String USER_AGENT = "Mozilla/5.0";
 +
 + public static void main(String[] args) throws Exception {
 +
 + HttpURLConnectionExample http = new HttpURLConnectionExample();
 +
 + System.out.println("\nTesting 2 - Send Http POST request");
 + http.sendPost();
 +
 + }
 +
 + // HTTP POST request
 + private void sendPost() throws Exception {
 +
 + String url = "http://example.com/main.do";
 + URL obj = new URL(url);
 + HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
 +
 + //add reuqest header
 + con.setRequestMethod("POST");
 + con.setRequestProperty("User-Agent", USER_AGENT);
 + con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
 +                con.setRequestProperty("Content-Type", "application/json");
 +
 +                //Petición JSON si tiene comillas, las trampeamos con \
 + String urlParameters = "{\"DatosPersonales\":{\"Nombre\":\"Aitor\",\"Apellido\":\"Tillas\"},\"DatosFisicos\":{\"Ojos\":\"Azules\",\"Pelo\":\"Negro\"}}";
 +
 + // Send post request
 + con.setDoOutput(true);
 + DataOutputStream wr = new DataOutputStream(con.getOutputStream());
 + wr.writeBytes(urlParameters);
 + wr.flush();
 + wr.close();
 +
 + int responseCode = con.getResponseCode();
 + System.out.println("\nSending 'POST' request to URL : " + url);
 + System.out.println("Post parameters : " + urlParameters);
 + System.out.println("Response Code : " + responseCode);
 +
 + BufferedReader in = new BufferedReader(
 +         new InputStreamReader(con.getInputStream()));
 + String inputLine;
 + StringBuffer response = new StringBuffer();
 +
 + while ((inputLine = in.readLine()) != null) {
 + response.append(inputLine);
 + }
 + in.close();
 +
 + //print result
 + System.out.println(response.toString());
 + }
 +}
 +</code>
 +
 +====== Proyecto Out Of Memory OOM ======
 +<code>
 +public class TestOOM
 +{
 +public static void main(String ar[])
 +{
 +String test[]=new String[Integer.MAX_VALUE];
 +for(int i=0;i<Integer.MAX_VALUE;i++)
 +{
 +test[i]="AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
 +}
 +}
 +}
 +
 +</code>
 +
  
 ====== Monitorizar con javamelody ====== ====== Monitorizar con javamelody ======
informatica/linux/java/maven.1428956385.txt.gz · Last modified: 2016/06/28 12:54 (external edit)