To start, I do not know much about maven or even jar's in general...
So basically I have been given a jar that was not created with maven to use in my maven project
Inside it has in it some classes:
com/something/types/Foo.class
com/something/types/Bar.class
com/something/utils/Foou.class
com/something/utils/Baru.class
etc.
I have tried running the following:
mvn install:install-file -Dfile=something.jar -DgroupId=com.something.somethingelse \
-DartifactId=somethingelse -Dversion=1.0 -Dpackaging=jar -DgeneratePom=true
and it says build success and has created the following:
~/.m2/repository/com/something/somethingelse/somethingelse/1.0/somethingelse-1.0.jar
~/.m2/repository/com/something/somethingelse/somethingelse/1.0/somethingelse-1.0.pom
in my project's pom I have included the following:
<dependency>
<groupId>com.something.somethingelse</groupId>
<artifactId>somethingelse</artifactId>
<version>1.0</version>
</dependency>
In my project I import like so:
import com.something.types.Foo
import com.something.utils.Foou
etc.
But when I run mvn install on my project I receive errors like the following every time I try to use something from the jar?
[ERROR] {{ProjectClass.java}}[line,col] cannot find symbol
[ERROR] symbol: class Foo
[ERROR] location: class ProjectClass
Please help! I do not use eclipse.
This process worked fine. The errors ended up being mistakes in my java code. For example:
import com.something.types.Foou // my old code
import com.something.utils.Foou // working import
Related
I know that questions have been asked before, but it does not solve my problem in my school project.
I downloaded the JSON.simple jar file from https://mkyong.com/java/json-simple-example-read-and-write-json/, then move it to the src directory and import it.
In the src folder I also have Main.java, Manager.java and Peer.java, in which Manager and Peer uses the json.simple package.
I am using Intellj.
At src directory, I try to run javac Main.java, I get 14 errors including
import org.json.simple.parser.JSONParser;
^
.\Manager.java:95: error: cannot find symbol
Then I tried to provide the full path for the jar file: javac -cp C:\Users\yiges\Desktop\COMP90015\Project3\WhiteBoard\src\json-simple-1.1 Main.java
I get the 4 errors including:
imple-1.1 Main.java
Main.java:15: error: cannot find symbol
Manager manager = new Manager(port);
^
symbol: class Manager
location: class Main
Then I tried to compile all java files: javac -cp C:\Users\yiges\Desktop\COMP90015\Project3\WhiteBoard\src\json-s imple-1.1 *.java or simply javac *.java
I get again 14 errors including:
Manager.java:1: error: package org.json.simple does not exist
import org.json.simple.JSONObject;
^
How do I solve this issue?
Thanks for helping!
if you create the project with Maven you are going to avoid all the import and compile problems and you will focus only on code. If you are new to Maven, it´s simpler than you can think. To create the proyect see this short video
Working with Maven in IntelliJ IDEA
Then you can easily import JSON.simple library adding the following dependency to pom.xml file, inside tags:
<!-- https://mvnrepository.com/artifact/com.googlecode.json-simple/json-simple -->
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>
In real projects you are going to use Maven or Gradle, so the soon you learn that is better. If continue with errors even with Maven or you need to solve this without Maven, post a reproducer or even your project.
Problem solved.
It turns out that I should use the javac -cp .;json-simple-1.1.1.jar C:\Users\yiges\Desktop\COMP90015\Project3\WhiteBoard\src\Main.java command to compile the code
I am trying to understand what I am doing wrong with junit-tests in Maven with eclipse. Mainly I have been just following this "getting started guide" for Maven:
https://spring.io/guides/gs/maven/
But for me it is not completely working like that when it comes to junit.
To follow the exact folder-structure from the example I have named my packages under src in eclipse:
main.java.hello
test.java.hello
My test class GreeterTest.java is in the package test.java.hello and has this content:
package test.java.hello;
import static org.hamcrest.CoreMatchers.containsString;
import static org.junit.Assert.*;
import org.junit.Test;
import main.java.hello.Greeter;
public class GreeterTest {
private Greeter greeter = new Greeter();
#Test
public void greeterSaysHello() {
assertThat(greeter.sayHello(), containsString("Hello"));
}
}
In my pom.xml you find the dependency:
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
Unfortunately eclipse sais "the import org.junit cannot be resolved."
If change the scope of the dependency from test to compile I do not get this error message, but that would be not the way how the scope is intended to be used, is it?
What do I need to change that also eclipse understands, that this class is a test-class and therefore all dependencies are actually available?
Best,Nils
The issue appears to be related to where Eclipse thinks your source tree starts. You have put your code in src/main/java/hello and src/main/test/hello but Eclipse thinks the source tree starts at src. So it thinks the other folders are packages and has given your classes package names like main.java.hello.Greeter.
The quickest way to fix this is to run on the command line:
mvn eclipse:eclipse
which uses maven to fix your eclipse projects. It will automatically set the source root to be the correct values. When it completes, right click on the project and select Refresh to see the updated project.
To fix this manually, you can right click on the project and choose Build Path > Configure Build Path and, in the Source tab on the right, ensure that the entire src/main/java (right up to java!) is included as the source folder. Do this by clicking Add Folder and selecting java in the tree under src - main. Do the same for src/main/test. Usually maven projects include src/main/resources as a source folder too.
I'm getting a dependency issue with a jar I'm attempting to use.
I receive the following error
remote: [ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.3.2:compile (default-compile) on project sparq: Compilation failure: Compilation failure:
remote: [ERROR] /var/lib/openshift/55846322500446673d000007/app-root/runtime/repo/src/main/java/ServerQuery.java:[3,0] error: package com.github.koraktor.steamcondenser does not exist
remote: [ERROR] /var/lib/openshift/55846322500446673d000007/app-root/runtime/repo/src/main/java/ServerQuery.java:[8,8] error: cannot find symbol
remote: [ERROR] class ServerQuery
remote: [ERROR] /var/lib/openshift/55846322500446673d000007/app-root/runtime/repo/src/main/java/ServerQuery.java:[8,34] error: cannot find symbol
Here is the offending java file.
package helpers;
import com.github.koraktor.steamcondenser.*;
public class ServerQuery {
public static String getPlayers() {
SourceServer server = new SourceServer("66.150.155.152",27015);
server.initialize();
return server.toString();
}
}
I've added the following dependency to my pom.xml
<dependency>
<groupId>com.github.koraktor</groupId>
<artifactId>steam-condenser</artifactId>
<version>1.3.9</version>
</dependency>
I've also added the following action_hook pre_build script
mvn install:install-file -Dfile=./app-root/repo/steam-condenser.jar -DgroupId=com.github.koraktor -DartifactId=steam-condenser -Dversion=1.3.9 -Dpackaging=jar
Here's the GitHub for the project
https://github.com/koraktor/steam-condenser-java
And the website for it
http://koraktor.de/steam-condenser/usage/
Any ideas? Completely lost. Help much appreciated.
Since you are including the jar file in your project already, trying to manage the dependency with Maven seems like overkill. You should be able to put the jar file in your project's lib directory like this article describes. Then you won't need to configure that dependency in Maven at all.
I would use Maven for any dependencies that you want to download at the time your application is being built. Any jar files that you are checking into your project shouldn't need to be configured in Maven.
Just by looking at the question and the excerpts provided, I believe you are struggling with the basics of Maven. If I understand the situation correctly you have a library (which is not written by you):
once added to your project as a Maven dependency. This seems reasonable if you want to use it.
once the source code of the library incorrectly added to your own Java project.
remote: [ERROR] /var/lib/openshift/55846322500446673d000007/app-root/runtime/repo/src/main/java/ServerQuery.java:[3,0] error: package com.github.koraktor.steamcondenser does not exist
Based on the errormessage above it seems you have copied one of the sourcefiles from the library into your project. This is now causing problems. The main one is that without your source-folder (src/main/java) correctly representing the package structure declared in the source file (com.github...) it cannot be compiled. Hence the errormessage:
package com.github.koraktor.steamcondenser does not exist.
First of all you need to remove this copy-pasted file entirely an just use the dependency-management feature of Maven to get the library on classpath. After that just follow the examples given by the author of the library using the link you have already found (http://koraktor.de/steam-condenser/usage/).
Is there a nice easy way to find out the Maven information for a library specified in an import statement in Java code?
For example, if I see some random piece of Java code on the internet that looks useful (happens often!), and I want to copy it into a Maven project, how do I find out what that dependency information is (i.e. the groupId, artifactId and version) to place into the POM.xml file?
e.g. If I see this:
import wow.magiclibrary.net.*;
public class Magic
{
public static void main ( String[] args ) throws IOException
{
try
{
MagicLibrary ml = new MagicLibrary( args[0] );
ml.doSomethingAmazing();
...
... where can I find the information to put into the POM.xml file to make Maven download the wow.magiclibrary.net library as a dependency from whatever repository it comes from?
<dependency>
<groupId>wow.magiclibrary.net</groupId>
<artifactId>magiclibrary</artifactId>
<version>2.0.0</version>
</dependency>
Do I just have to use Google to search for a repo on the web that has the library, download jars manually and install them into my local maven repo?
Just curious. I'm lazy. I know I can do something like this after downloading a jar file:
mvn install:install-file -DgroupId=wow.magiclibrary -DartifactId=magiclibrary \
-Dversion=2.0.0 -Dpackaging=jar -Dfile=magiclibrary2.0.0.jar -DgeneratePom=true
... but if there was a way to automate the search and install process through Maven then that would rock.
When I'm looking for the maven dependency for a library I know usually go to
http://mvnrepository.com/
If I'm looking for a library I don't know, I would first do a simple google search or grepcode.com and then go to mvnrepository.com
mvnrepository allows you to easily copy paste the maven dependency, here is an example for commons.lang version 2.6
http://mvnrepository.com/artifact/commons-lang/commons-lang/2.6
You can use online tools like grepcode.com, which will identify you the jar names and maven artifacts. Here is an example for apache commons-lang StringUtils.
I have a little problem in Eclipse with a package . The error output is Package (name of package ) does not exist .
I have a package called de.baimos.blueid.lockserver.demo.exec . But Eclipse is looking for a package called de.baimos.blueid.lockserver.api.exec . This package is can be found in my Project nowhere .
My current situation is this: I have two projects that work as a project. Now you can in Eclipse under Properties - Add a project to a different > Project -> Java Build Path . But if I want to run Maven install I get the error above. At first I thought it would be in the pom.xml file . But I noticed that it is not possible to merge two pom.xml files. My project was to make the inheritance , however, by themselves , whom I put them together leads . Did anyone of you ever such a problem , or can someone help me ?
Thanks in advance for your help .
That is the error:
[ERROR] /home/test/workspace/HeartbeatService/src/main/java/de/baimos/blueid/lockserver/demo/exec/DemoCommandExecutionEventListener.java:[3,44] error: package de.baimos.blueid.lockserver.api.event does not exist
[ERROR] /home/test/workspace/HeartbeatService/src/main/java/de/baimos/blueid/lockserver/demo/exec/DemoCommandExecutionEventListener.java:[4,44] error: package de.baimos.blueid.lockserver.api.event does not exist
[ERROR] /home/test/workspace/HeartbeatService/src/main/java/de/baimos/blueid/lockserver/demo/exec/DemoCommandExecutionEventListener.java:[5,44] error: package de.baimos.blueid.lockserver.api.event does not exist
[ERROR] /home/test/workspace/HeartbeatService/src/main/java/de/baimos/blueid/lockserver/demo/exec/DemoCommandExecutionEventListener.java:[6,44] error: package de.baimos.blueid.lockserver.api.event does not exist
[ERROR] /home/test/workspace/HeartbeatService/src/main/java/de/baimos/blueid/lockserver/demo/exec/DemoCommandExecutionEventListener.java:[8,59] error: cannot find symbol
Maven can't see what you specify in Eclipse's Java Build Path; it's a command line tool that runs outside / without Eclipse.
Instead, you have to do mvn install in the first project.
Then you can add a dependency to this project in the second project's POM.
Maven will then make sure that Eclipse add the first project to the classpath as well without manually changing the build path in the UI.