I want to print out a String that contains Cyrillic letters (for the sake of example). The problem is that on my output the string will show '?' characters instead. This problem only occurs if I try to print on stdout in a (fresh or not) Neatbeans Maven project.
Below you can see that Maven puts the "-Dfile.encoding=UTF-8" parameter in, so I dont understand why is my encoding not UTF-8. When I create a fresh Neatbeans project (without Maven) the encoding shows UTF-8 and the output is fine.
package com.trashmaven;
import java.nio.charset.Charset;
public class TrashMaven {
public static void main(String[] args) {
System.out.println("file.encoding=" + System.getProperty("file.encoding"));
System.out.println("defaultCharset: " + Charset.defaultCharset());
String myString = "мусор";
System.out.println("Russian letters: " + myString);
}
}
My pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com</groupId>
<artifactId>TrashMaven</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>10</maven.compiler.source>
<maven.compiler.target>10</maven.compiler.target>
</properties>
</project>
The output is the following:
cd C:\Asztal\0Java Projekts\Testing stuff\TrashMaven; "JAVA_HOME=C:\\Program Files\\Java\\jdk-10.0.2" "M2_HOME=C:\\Program Files (x86)\\Maven\\apache-maven-3.6.0" cmd /c "\"\"C:\\Program Files (x86)\\Maven\\apache-maven-3.6.0\\bin\\mvn.cmd\" -Dexec.args=\"-classpath %classpath com.trashmaven.TrashMaven\" -Dexec.executable=\"C:\\Program Files\\Java\\jdk-10.0.2\\bin\\java.exe\" -Dmaven.ext.class.path=\"C:\\Asztal\\0Java Projekts\\00netbeans\\netbeans\\java\\maven-nblib\\netbeans-eventspy.jar\" -Dfile.encoding=UTF-8 org.codehaus.mojo:exec-maven-plugin:1.5.0:exec\""
Running NetBeans Compile On Save execution. Phase execution is skipped and output directories of dependency projects (with Compile on Save turned on) will be used instead of their jar artifacts.
Scanning for projects...
---------------------------< com:TrashMaven >---------------------------
Building TrashMaven 1.0-SNAPSHOT
--------------------------------[ jar ]---------------------------------
--- exec-maven-plugin:1.5.0:exec (default-cli) # TrashMaven ---
file.encoding=Cp1252
defaultCharset: windows-1252
Russian letters: ?????
------------------------------------------------------------------------
BUILD SUCCESS
Set the environment variable JAVA_TOOL_OPTIONS=-Dfile.encoding=UTF8, restart Netbeans and try again. Check the value "defaultCharset" that you're logging (It should be UTF-8 then).
Related
I built a server template in java and now i need to compile and run it using MAVEN.
this is my POM:
<?xml version="1.0" encoding="UTF-8"?>
-<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0">
<modelVersion>4.0.0</modelVersion>
<groupId>bgu.spl</groupId>
<artifactId>spl-net</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
-<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<name>spl-net</name>
</project
When I run mvn compile it all works. but then i go to test it with: mvn exec:java - Dexec.mainClass=”bgu.spl.net.impl.BGRSServer.ReactorMain” - Dexec.args=”<port> <No of threads>” and it gives me a class not found exception. I looked in the target directory and this exact main was indeed there, it just refuses to run.
Turns out there was nothing wrong with my code. I failed to realize that in ubuntu, the command: mvn exec:java - Dexec.mainClass=”bgu.spl.net.impl.BGRSServer.ReactorMain” - Dexec.args=”<port> <No of threads>” simply doesn't work. the correct command would be: mvn exec:java - Dexec.mainClass=bgu.spl.net.impl.BGRSServer.ReactorMain - Dexec.args=<port> <No of threads>
I have source code for a framework, let's call it my-framework. It provides various packages, including com.not_telling.framework.db.
I have a pom.xml file for this framework:
<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 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.not_telling</groupId>
<artifactId>my-famework</artifactId>
<packaging>jar</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>my-framework Maven Library</name>
<url>http://maven.apache.org</url>
<!-- more things here... -->
</project>
I can compile this and install it to my local repository cache, using this command:
mvn install
End of the install log:
[INFO] Installing C:\Users\MyUserName\my-framework\backend\target\my-framework.jar to C:\Users\MyUserName\.m2\repository\com\not_telling\my-famework\0.0.1-SNAPSHOT\my-famework-0.0.1-SNAPSHOT.jar
[INFO] Installing C:\Users\MyUserName\my-framework\backend\pom.xml to C:\Users\MyUserName\.m2\repository\com\not_telling\my-famework\0.0.1-SNAPSHOT\my-famework-0.0.1-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
Then I have another project, that tries to use this as a dependency:
<dependency>
<groupId>com.not_telling</groupId>
<artifactId>my-famework</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
The compilation phase of this project fails with this message:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.1:compile (default-compile) on project my-project: Compilation failure: Compilation failure:
[ERROR] /C:/Users/MyUserName/workspace/my-project/backend/src/main/java/com/not_telling/some_example.java:[13,17] package com.not_telling.framework.db does not exist
Why is that? Maybe because the jar is in the repository cache, but not in a local repository?
This question is a follow up of absolute maven pomderived entry added to classpath - I realized that instead of doing magic with relative source directory references, I need to make a separate module, but I'm facing this problem.
It was bleeding from numerous wounds. These needed to be fixed:
move source files from under src/ to under src/main/java, in both the framework and the application project
change the group and the artifact id to match the package name E.g. for a package that is named "com.not_telling.framework" I had to use groupId=com.not_telling and name=framework.
The dependency scope was wrong in the application's pom file. I had to remove test
Now it works from command line I can do "mvn install" for the framework, and then "mvn package" for the applications.
The only problem left is that the application cannot be compiled from eclipse, I had to remove all linked source folders / build paths in the application project to the framework.
I am looking to start a project using Vaadin and I want to use Maven as the repository manager, but I am having a lot of trouble starting the Maven project.
I tried creating a project in IntelliJ using the vaadin-archetype-application-example and it doesn't create any directory.
I also tried creating the project with the mvn commands and I found a lot of troubles too. When I create the Maven project this error is shown in the log:
-Dmaven.multiModuleProjectDirectory system propery is not set. Check $M2_HOME environment variable and mvn script match.
[ERROR] Maven execution terminated abnormally (exit code 1)
How can I deploy a Maven project with IntelliJ? I have the Maven plugin installed and the mvn command seems to work just fine.
This is my pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.imtoolazytoadmin</groupId>
<artifactId>ImTooLazyToAdmin-master</artifactId>
<version>1.0-SNAPSHOT</version>
</project>
Use this command to create your project and then import it to your IDE.
$ mvn archetype:generate \
-DarchetypeGroupId=com.vaadin \
-DarchetypeArtifactId=vaadin-archetype-application \
-DarchetypeVersion=7.5.9 \
-DgroupId=your.company \
-DartifactId=project-name \
-Dversion=1.0 \
-Dpackaging=war
More info about creating Vaadin projects in IntelliJ here.
[ SOLVED ] - but still looking for explanation. Please see bottom of question
I don't have much experience with command line or with Maven. I'm working on a tutorial from a book. It state I should create a java file
src/main/java/com/effectivemaven/chapter01/ExampleAction.java
What I did was mkdir each directory separately i.e. mkdir com, mkdir effectivemaven, mkdir chapter01
I create the .java file in the chapter01 directory.
package com.effectivemaven.chapter01;
import org.slf4j.*;
public class ExampleAction {
final Logger logger = LoggerFactory.getLogger(ExampleAction.class);
public boolean execute() {
logger.info( "Example Action Executed." );
return true;
}
}
When I mvn compile, it says compiling 1 file to target..., but when I look at the taget directory, nothing is created.
So I tried to create a another .java file without using packages, just a simple
public class Hello {
public static void main(String[] args) {
System.out.println("Hello");
}
}
in the java directory, then mvn compile and it shows up in the target file.
So my assumption is that I'm not creating the packages correctly by using mkdir, or possibly I'm doing something else wrong I'm unaware of.
So basically I just want to know, what is the correct way to create a package from the command line? And if I'm doing it correctly, what could be the other possible reasons the .class is not being created in the target?
EDIT tree
first-webapp
src
main
java
Hello.java
com
effectivemaven
chapter01
ExampleAction.java
target
classes
Hello.class
pom.xml
Command running from C:\Maven Book\first-webapp>
C:\Maven Book\first-webapp>mvn compile
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building first-webapp Maven Webapp 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) # first-weba
pp ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources,
i.e. build is platform dependent!
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:2.5.1:compile (default-compile) # first-webapp
---
[WARNING] File encoding has not been set, using platform encoding Cp1252, i.e. b
uild is platform dependent!
[INFO] Compiling 2 source files to C:\Maven Book\first-webapp\target\classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.667s
[INFO] Finished at: Sun Jan 12 00:09:49 PST 2014
[INFO] Final Memory: 11M/111M
[INFO] ------------------------------------------------------------------------
C:\Maven Book\first-webapp>
EDIT pom.xml as requested
<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 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.underdogdevs.webapp</groupId>
<artifactId>first-webapp</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>first-webapp Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.5</version>
</dependency>
</dependencies>
<build>
<finalName>first-webapp</finalName>
</build>
</project>
By the way, this is a web-app I created with the following command
mvn archetype-generate -DgroupId=com.underdogdevs.webapp -DartifactId=first-webapp -DarchetypeArtifactid=maven-archetype-webapp
[ SOLVED ] - but with very mimimal understanding - still offering brownie points for anyone who can explain to me this behavior. Below is what I did
I got it to work. Since I create the project with groupId=com.underdogdevs.webapp, I tried to make a package com\underdogdevs\webapp and created a the class with the corresponding package reference. This fixed the problem. The class appears. But I tested it even further and deleted the package I just created and tried to clean and compile with only the original package structure, but the file showed up again in the orginal package structure. I have no idea why this happens though.Anyone have any ideas?
Ok, the groupid isn't related to your application structure -- it's part of your application name. You are building a webapp, so the compiled classes go into the WEB-INF/classes directory in the generated war file (which, according to your pom is called first-webapp.war) which can be found in the target directory. You need directories src/main/java, src/main/resources, src/test/java, src/test/resources, src/main/webapp/WEB-INF. Under the java directory you should put your package structure (more directories), which in this case is com/effectivemaven/chapter01 and under that put your java file ExampleAction.java. Put your pom in your project root.
On the command line run mvn clean install and you'll generate the target and you should find your .war file in there. In that, under the WEB-INF/classes/com/effectivemavem/chapter01 directory you'll find your compiled java class with a .class extension.
At some point you'll have to put a web.xml file in the WEB-INF directory.
I start learning packaging for several distros (currently Cygwin and Debian).
They have requirement to build system to allow out-of-tree build (synonym out-of-source build):
http://wiki.debian.org/UpstreamGuide#Out-of-Tree_Builds
To work-around "dumb" build system for example cygport recommend use lndir (from xutils project):
lndir ${S} ${B}
cd {B}
...build-commands...
I read mvn(1) man page but doesn't found anything appropriated. Next I just try:
$ mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=my-app -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
...
$ pwd
/maven/simple
$ ls
my-app
$ mvn -f my-app/pom.xml compile
...
[INFO] --- maven-compiler-plugin:2.0.2:compile (default-compile) # my-app ---
[INFO] Compiling 1 source file to /maven/simple/my-app/target/classes
As you can see target directory created in source root hierarchy while I look for a way to avoid this.
Is it possible out-of-tree build with maven? And how?
You could do like this to get it in your current working directory:
<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.stackoverflow</groupId>
<artifactId>Q13173063</artifactId>
<version>1.0-SNAPSHOT</version>
<name>${project.artifactId}-${project.version}</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<buildDir>${user.dir}</buildDir>
</properties>
<build>
<directory>${buildDir}</directory>
</build>
</project>
Then you can issue
mvn -f my-app/pom.xml compile
And it will give you your classes in the current working directory.
And easily change to another output directory:
mvn -f my-app/pom.xml -DbuildDir=/tmp/build compile
It might be as simple as having a
<build>
<directory>/your/build/directory</directory>
</build>
in your pom.xml. /your/build/directory need not be in the source tree and can be parameterized using the usual ${...} syntax.
Cheers,