Java importing packages - java

I have a program in my desktop that I want to run (structure in the url), but when I do compile, with
\code\nlp\assignments\parsing\javac PCFGParserTester.java
I get:
PCFGParserTester.java:6: error: package nlp.io does not exist
import nlp.io.PennTreebankReader;
^
PCFGParserTester.java:7: error: package nlp.ling does not exist
import nlp.ling.Tree;
^
PCFGParserTester.java:8: error: package nlp.ling does not exist
import nlp.ling.Trees;
^
PCFGParserTester.java:9: error: package nlp.parser does not exist
import nlp.parser.EnglishPennTreebankParseEvaluator;
how do I get my program to correctly import my packages?

You want to be in the \code directory and compile with:
javac nlp\assignments\parsing\PCFGParserTest.java
(And you should have a package declaration of package nlp.assignments.parsing; to match the position in the directory structure.)
That way javac will look for the other classes appropriately.
Alternatively, and rather more simply, you could use an IDE such as Eclipse or NetBeans, and it would take care of all this for you - you'd just specify the code directory as a source directory, and all would be well.

Related

Java compiler doesn't see sibling packages

From Windows command line, I am trying to compile a java file in package com.example.web that imports a class from package com.example.model but keep getting the message that the package com.example.model does not exist.
From what I can tell, the compiler doesn't recognize sibling packages for some reason, as I do not get the error message when I try to import the package com.example but I do get it if I try to import com.example.test. I've compiled the files in the other packages that are not dependent on imports without an issue from the same directory. I'm almost positive that I've had access to sibling packages from the command line in the past.
package com.example.web;
import com.example.*;
import com.example.test.*;
import com.example.model.*;
public class Test{
}
I get 2 errors saying that package com.example.model and com.example.test do not exist. They're in my file structure, they have .class files in them.
UPDATE: It works if I add com.example.model and com.example.test to my classpath, but I was under the impression this was unnecessary if the dependencies shared the same parent folder as the class I was compiling. Am I mistaken? Did this use to be the case but changed in the last few years? It's been awhile since I've compiled directly from command line, but I don't remember having to do this before.

SBT project with a Java module in IntelliJ

I am learning developing project in IntelliJ.
I started a new SBT project. Now I have two files, one is a Scala script projectName/src/main/scala/scalaScript.scala and the other is a Java class projectName/src/main/java/moduleName/MyClass.java
The Scala script is used for me to test codes line-by-line in a Scala Console. In oder to test the class I defined in MyClass.java, I think I need to compile it first and then import moduleName.MyClass in the Scala script. After that, I can use the java class to create objects and do whatever I want.
If I am not using IntelliJ, I just need to go to the java module directory and call javac -cf myJavaLibrary.jar MyClass.java to create a jar. Then move myJavaLibrary.jar into the same directory as that of my Scala script file.
How do I efficiently do that in IntelliJ? What is the working pipeline?
Update 1
I switched to using Scala worksheet. However, after import moduleName.MyClass, there is an error saying the module is not found.
import bestbuy.Laptop
where bestbuy is the module name, and Laptop is a class. Then the message says:
Error:not found: value bestbuy
import bestbuy.Laptop
^
Error:(6, 10) not found: value bestbuy
;import bestbuy.Laptop
^
Error:(18, 50) not found: value bestbuy
println(MacroPrinter211.printImportInfo({import bestbuy.Laptop;}))
^
Error:(26, 50) not found: value bestbuy
println(MacroPrinter211.printImportInfo({import bestbuy.Laptop;}))
^
Update 2
In the worksheet, you should check the little box in front of "Make project" so that all the source code will be compiled and the byte code will be saved in the target directory where SBT expects. Then I can successfully import bestbuy.Laptop in the worksheet.
However, I am still expecting an answer to my original question. If I can import bestbuy.Laptop in the Scala script file instead of a worksheet. In the worksheet all codes are executed at once, while I would like to execute them one line at a time.
After the project is compiled, I tried executing import bestbuy.Laptop in the Scala Console, but got error:
scala> import bestbuy.Laptop
<console>:13: error: not found: value bestbuy
import bestbuy.Laptop
^
It seems that the console's class path is not properly configured. How can I configure the console's class path?
to use repl-like environment in IntelliJ just create new scala worksheet file and in there you can import your Java class and use it. IDE will compile everything for you.
You can also use terminal, type in sbt console and you will have your repl with your project classes in classpath, so you can import and use them.

how to import joda time in java on Ubuntu system

I'd like to generate dates sequence in a range. This thread suggests to use Joda-Time package. I downloaded it and unzipped it to the same directory as my Main.java.
When I try import ./joda-time-2.7/org.joda.time.DateTime;, the compiler says:
Main.java:4: error: expected
import ./joda-time-2.7/org.joda.time.DateTime;
^
Main.java:4: error: expected
import ./joda-time-2.7/org.joda.time.DateTime;
^
Main.java:4: error: class, interface, or enum expected
import ./joda-time-2.7/org.joda.time.DateTime;
^
And when I try import org.joda.time.DateTime;, the compiler says:
MissingDateSearch.java:5: error: package org.joda.time does not exist
import org.joda.time.DateTime;
^
It seems that I didn't include the package into my building path. This thread discusses how to set the building path for java. Currently, my "environment" file only has PATH variable. I don't want to make any global change just for one project.
So my question is, does any one know how to include the package in a way without global change? Or does any one has a simple way to generate dates in a range without joda-time?
Thanks!
ADDED
This problem can be bypassed by using IDEs like eclipse. Any terminal based solutions are still welcomed.
Use the
import org.joda.time.DateTime;
import statement and ensure that JodaTime jar file is on your classpath at compile & runtime

setting the correct classpath for compiling and running Java packages? [duplicate]

This question already has an answer here:
getting error when I compile the Java code using package in commandline?
(1 answer)
Closed 8 years ago.
I have been using Eclipse lately, where compiling and running the program is very simple. Not much needs to be done in setting the classpath. But apparently that is not the case when it comes to running them from commandLine. when I try compiling from terminal, I am having various errors. I am pasting an image of my package structure of the project cp125_soln. Now I want to compile Invoice.Java in the com.scg.domain package,
I tried
javac src/main/java/com/scg/domain/Invoice.java
src/main/java/com/scg/domain/Invoice.java:17: error: package com.scg.util does not exist
import com.scg.util.StateCode;
.......................//long error message
This means I do not have com.scg.util.* in my classpath. so I tried
javac -cp src/main/java/com/scg/util/* src/main/java/com/scg/domain/Invoice.java
src/main/java/com/scg/util/ListFactory.java:8: error: package org.slf4j does not exist
import org.slf4j.Logger;
^
src/main/java/com/scg/util/ListFactory.java:9: error: package org.slf4j does not exist
import org.slf4j.LoggerFactory;
^
src/main/java/com/scg/util/ListFactory.java:11: error: cannot find symbol
import com.scg.domain.ClientAccount;
^
symbol: class ClientAccount
location: package com.scg.domain
................... // long error message
I read different articles on how classpath works and how to provide it in command-line. but when it comes topackage level structures, I am not able to find a good tutorial on how to COMPILE and RUN packages. If a little help could be provided here on the propery way to compile and run these kind of packages, it will be very helpful.
javac src/main/java/com/scg/domain/Invoice.java
Try this:
cd src/main/java
javac com/scg/domain/Invoice.java

Java using classes from jar

This must be a super overasked question. Although here goes:
I have a java file for testing around (hworld.java) and am trying to import conio.jar, a JAR which is a wrapper of Conio. The JAR contains only one class file (conio.class) and META-INF. Trying to do import conio.* or import conio.conio shows me this:
C:\Documents and Settings\Nick\Desktop>javac -cp *.jar; hworld.java
hworld.java:3: error: package conio does not exist
import conio.*;
^
1 error
And compiling it like javac -cp conio.jar hworld.java still errors out while compiling. I even extracted the jar and had conio.class in the same directory as hworld.java but to no avail. The JAR is in the same directory as hworld.java, as well.
Anyone have any idea on how to fix this?
You don't mention whether conio.class is defined in package conio. If it is not, then simply use the class without importing it. Remove the import.
It's actually not possible. You need to put the other class in a package if you want to import it.
What's the syntax to import a class in a default package in Java?
Find out what package Conio is in - an easy way to do this is to open the jar as a zip file, the package will correspond with the folder structure of the archive. For example if Conio is in x/y/z then import x.y.z.Conio and compile/run with conio.jar on the classmate.

Categories