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
Related
I am trying to modify WEKA source code.
I tried the link on modify weka source code in netbeans but is no longer working. I am trying to run the WEKA java source code in Intellij and I get 100 errors (literally) coming from the weka, like
Error:(23, 44) java: package com.googlecode.jfilechooserbookmarks does not exist
Error:(28, 1) java: package no.uib.cipr.matrix does not exist
and it goes on.
I tried to import it as a Maven project because of the pom.xml dependencies. It compiles and builds without any problem, but when I try to run a Java class, ex. Main.java, it gives all the errors above.
My project structure is:
Project
-scr
--main
---java (Source Folder)
----Main.java
-weka-src
--src
---main
----java (Source Folder)
------weka
Does anyone have an idea of how it can compile without errors?
Thank you in advance!
Thanks Jens for the comment!
Indeed, there were some dependencies I needed to add to pom.xml. Now it's working. This is what I added:
<dependency>
<groupId>com.googlecode.jfilechooser-bookmarks</groupId>
<artifactId>jfilechooser-bookmarks</artifactId>
<version>0.1.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.github.vbmacher/java-cup-runtime -->
<dependency>
<groupId>com.github.vbmacher</groupId>
<artifactId>java-cup-runtime</artifactId>
<version>11b</version>
</dependency>
I'm trying to write a simple ant build to compile a project.
The project is in eclipse and there it compiles successfully (with the eclipse-compiler).
But with ant (using javac) it appears an error and i don't know how to resolve it.
Structure of the used jar:
com
xxx
a <= package
b
a.class
Codeblock of my class:
Object o = com.xxx.a.b.method();
^
The exception of ant is:
error: cannot find symbol
symbol: variable b
location: class a
I think eclipse uses the package first to try to compile the code. javac seems to think that a is the class.
Is there a way to resolve the problem without changing the jar?
It looks like either package name is different or you have multiple class files of the same name. I would suggest checking the import statements and adding the specific jar file to classpath while compiling using javac or ant command.
To find the exact jar file, use ctrl+T then paste your class name in the box and it will tell you the jar file. Add that jar file to your ant classpath and build.
I didn't find anything in the Java Language Specification that this is an error, so it might be a javac bug.
Since it is a javac vs. Eclipse compiler thing, try one of the following:
Use the Eclipse compiler in the Ant script
If it is a javac bug, the bug may be fixed in a newer (update) JDK version
If your code does not directly reference class com.xxx.a, compile the code with the JAR in which the class com.xxx.a was removed
I have the following directory structure:
project/hamcrest-core-1.3.jar
project/junit-4.12.jar
project/build/
project/ija/ija2016/HomeWork2Test.java
project/ija/ija2016/homework2/model/cards/Card.java
project/ija/ija2016/homework2/model/cards/CardDeck.java
project/ija/ija2016/homework2/model/cards/CardStack.java
project/ija/ija2016/homework2/model/board/AbstractFactorySolitaire.java
project/ija/ija2016/homework2/model/cards/FactoryKlondike.java
The HomeWork2Test.java was given to us (I cannot edit this one) and contains tests for the other classes. In the header it has these imports:
package ija.ija2016.homework2;
import ija.ija2016.homework2.model.board.AbstractFactorySolitaire;
import ija.ija2016.homework2.model.board.FactoryKlondike;
import ija.ija2016.homework2.model.cards.Card;
import ija.ija2016.homework2.model.cards.CardDeck;
import ija.ija2016.homework2.model.cards.CardStack;
So I made the Card, CardStack and CardDeck classes into a package by specifying:
package ija.ija2016.homework2.model.cards;
in each of the files.
And the AbstractFactorySolitaire and FactoryKlondike have:
package ija.ija2016.homework2.model.board;
Now we are supposed to run the tests in the HomeWork2Test.java class using JUnit. However, when I try to run the following command from the project folder:
javac -cp junit-4.12.jar -d build ija/ija2016/homework2/HomeWork2Test.java
I get errors telling me that the:
package.ija2016.homework2.model.cards does not exist
package.ija2016.homework2.model.board does not exist
I don't exactly understand how to fix the project structure. Also how do I run the JUnit test?
Thank you for replies.
Well I think you are mixing concepts (compiling and running junit tests)
1st you need to compile your Classes (let's compile them to build
dir):javac -d build ija/ija2016/homework2/model/cards/*.java ija/ija2016/homework2/model/board/*.java
2nd you need to compile your test class (you will need to add to classpath what you just compiled and the junit.jar dependency): javac -d build -cp build;junit-4.12.jar ija/ija2016/HomeWork2Test.java
Now you can run your test class (in order to run you need to add to the classpath the build dir and the jar dependencies): java -cp build;junit-4.12.jar;hamcrest-core-1.3.jar org.junit.runner.JUnitCore ija.ija2016.HomeWork2Test
If you want to know more check JUnit 4 Doc
if you are using Linux or MacOS use ":" instead ";" between dirs in classpath
I am trying to run a simple doclet program, but I am not able to compile it.
javac -cp /cygdrive/c/Progra~2/Java/jdk1.8.0_65/lib/tools.jar A.java
But it throws
A.java:1: error: package com.sun.javadoc does not exist import
com.sun.javadoc.ClassDoc;
Where A.java is
import com.sun.javadoc.ClassDoc;
public class A {
}
I referred it from
http://download.java.net/jdk7u2/docs/technotes/guides/javadoc/doclet/overview.html
I know that I am doing a simple mistake but I ma not able to figure it out.
Can anyone please point me out what am I doing wrong
You need to add Tools.jar to the project path. It is not included in the standard installation.
Can I ask why you need com.sun.javadoc? It is in most cases discouraged to use com.sun overall.
I am trying to call a python method in java, similar functionality as Unresolved import org.python / working with jython and java?
However, I am using ant to compile and run my files. using import org.python.util will give me this error
package org.python.util does not exist
I can see that python.org.util exists in jython-2.5.0.jar.
So, here is the class path I have in my build.xml ant file:
classpath="${java.class.path}:./jgrapht/lib/jgrapht-jdk1.5.jar:\
./jgrapht/lib/jgraph.jar:./jgraphx/lib/jgraphx.jar:\
./jython/lib/jython-2.5.0.jar:./colt/lib/colt.jar:."
and I also I added the path to jython jar files to my class path. i.e. looks like echo $path gives me all the required paths. Is there anything missing here that I am not aware of?
Try this to make all classes in the package available:
import org.python.util.*;
Or this to include a particular class:
import org.python.util.TemplateAntTask;
EDIT: Also, looks like there is an extra slash after jython-2.5.0.jar in your classpath.