I have a following directory structure:
-com
-laicode
-class1
-QuickSort.java
-common
-someclasses
-test
-class1
-QuickSortTests.java
I want to import QuickSort class and classes in common package in QuickSortTests. So the code in QuickSort.java looks like:
package com.test.class1;
import java.util.Arrays;
import com.laicode.common.*;
import com.laicode.class1.QuickSort;
class QuickSortTests {
public static void main(String[] args) {
int[] array0 = null;
QuickSort.quickSort(array0);
System.out.println(Arrays.toString(array0));
int[] array1 = new int[0];
QuickSort.quickSort(array1);
...
When I complie QuickSortTests.java in cmd using javac QuickSortTests.java, it throws an error saying:
QuickSortTests.java:4: error: package laicode.common does not exist
import laicode.common.*;
QuickSortTests.java:5: error: package laicode.class1 does not exist
import laicode.class1.QuickSort;
But in Intellij, QuickSortTests can run without any errors.
IntelliJ already knows your source layout, and even what classes are defined in what files. javac doesn't. There are multiple ways you could approach the problem, depending in part on where you want the compiled class files to go, but the simplest approach would probably be to make the root of your source tree your working directory, and run javac from there:
javac com/test/class1/QuickSortTests.java
(Or use backslashes instead of forward slashes on Windows.)
Note capitalization: I have assumed that the mismatch between the capitalization of your class names and that of the names of the files in which they reside is an error in your question. If these actually do disagree in your sources then you should fix the discrepancy.
Related
I am trying to import a jar file. My file "Test.java" contains the line:
"import org.jfugue.*;"
When I run the command "javac -classpath .:jfugue-5.0.9.jar Test.java", I get the error "package org.jfugue does not exist". How do I fix this?
Note: I am using a Mac machine.
Actually if you inspect the jar file "jfugue-5.0.9.jar", There're no any Class files in the package "org.jfugue.". Instead it contains some sub packages such as org.jfugue.devices., org.jfugue.integration., org.jfugue.parser. etc.
Try something like this,
import org.jfugue.devices.*;
public class Hello {
public static void main (String[] args) {
System.out.println("Hi");
}
}
Starting point
You want to compile code using the contents of a jar file, specifically "jfugue-5.0.9.jar", and you have a "Test" class with an import statement, like this:
import org.jfugue.*;
public class Test {
}
If you compile that code, you get an error like this:
% javac -classpath .:jfugue-5.0.9.jar Test.java
Test.java:1: error: package org.jfugue does not exist
import org.jfugue.*;
^
1 error
What's going on?
You're doing the right steps, mostly, but the import statement isn't correct. Syntax-wise, it's fine, but it does not align with the contents of the jar file. The structure of the jar contents (which you can see by running: jar tf jfugue-5.0.9.jar) shows that there is a directory for "org/jfugue/", but there are no classes or interfaces there; it's just a directory.
Below is a view of the first 9 lines of jar contents, sorted. It shows several directories without file
contents – "org/" and "org/jfugue/" – but "org/jfugue/devices/" for example has four files present.
% jar tf jfugue-5.0.9.jar | sort | head -9
META-INF/
META-INF/MANIFEST.MF
org/
org/jfugue/
org/jfugue/devices/
org/jfugue/devices/MidiParserReceiver.class
org/jfugue/devices/MusicReceiver.class
org/jfugue/devices/MusicTransmitterToParserListener.class
org/jfugue/devices/MusicTransmitterToSequence.class
So if you were to change the import statement to "org.jfugue.devices.*" – which would match those four files
("MusicReceiver", etc) – then compilation would work fine (no errors).
import org.jfugue.devices.*;
public class Test {
}
% javac -classpath .:jfugue-5.0.9.jar Test.java
%
Solution
Following
JLS 7.5.1,
you can import each specific class one by one, such as:
import org.jfugue.devices.MidiParserReceiver;
import org.jfugue.devices.MusicReceiver;
import org.jfugue.devices.MusicTransmitterToParserListener;
import org.jfugue.devices.MusicTransmitterToSequence;
Or following
JLS 7.5.2,
you can import all classes and interfaces matching a wildcard pattern (so
long as there are actually classes or interfaces matching that pattern)
such as:
import org.jfugue.devices.*;
It's not allowed to import a subpackage, so "import org.jfugue;" (without the .* wildcard) would not work
(see Example 7.5.1-3 No Import of a Subpackage in JLS).
I'm trying to complete the first assignment of the Coursera course Algorithms I. As I'm using Linux, I followed the instructions on http://algs4.cs.princeton.edu/linux/ and created a ~/algs4 directory. The test programs seem to work; for example, the command
kurt#kurt-ThinkPad:~$ java-algs4 edu.princeton.cs.algs4.StdDraw
generates the image below.
Next, I'm trying to run a test program in a subdirectory of ~/algs4:
import edu.princeton.cs.algs4.In;
import edu.princeton.cs.algs4.StdDraw;
public class PercolationTest {
public static void main(String[] args) {
System.out.println("Hello, world!");
}
}
However, if I try to compile this I get the following errors:
kurt#kurt-ThinkPad:~/algs4/PercolationTest$ javac PercolationTest.java
PercolationTest.java:3: error: package edu.princeton.cs.algs4 does not exist
import edu.princeton.cs.algs4.In;
^
PercolationTest.java:4: error: package edu.princeton.cs.algs4 does not exist
import edu.princeton.cs.algs4.StdDraw;
^
2 errors
This way of importing does seem consistent with what is explained on http://algs4.cs.princeton.edu/code/, so I'm having trouble seeing what I did wrong. Any ideas?
Following the examples on http://algs4.cs.princeton.edu/code/, I appended -algs4 to both the javac and java commands. Now it seems to work:
kurt#kurt-ThinkPad:~/algs4/PercolationTest$ javac-algs4 PercolationTest.java
kurt#kurt-ThinkPad:~/algs4/PercolationTest$ java-algs4 PercolationTest
Hello, world!
I have a package named "shapefile" which contains classes IndexFileReader and IndexFile. IndexFileReader returns an object of type IndexFile.
I then have a class named ReadFiles which imports "shapefile" and uses IndexFileReader to create an IndexFile.
import java.io.IOException;
import shapefile.*;
public class ReadFiles {
public static void main(String[] args) throws IOException{
IndexFile indexFile;
indexFile = IndexFileReader.readIndexFile("FilePath/FileName.shx");
}
}
My files are located in folders as follows:
JavaProjects
shapefile
IndexFileReader.java
IndexFile.java
MyProject
ReadFiles.java
I compile the shapefile project from the Windows command line like this and it compiles fine:
cd JavaProjects
javac .\shapefile\*.java
I compile my project like this and I get an error:
cd MyProject
javac -cp .. ReadFiles.java
Error:
ReadFiles.java:8: error: incompatible types
indexFile = IndexFileReader.readIndexFile("FilePath/FileName.shx");
^
required: IndexFile
found: shapefile.IndexFile
This is new to me and I believe I am doing something wrong with running javac. I have successfully compiled ReadFiles previously, I thought I did so with the same commands I've printed here. I actually still have the ReadFiles.class file from when this compiled successfully and it runs without any problems, which is why I believe I am not using javac correctly.
Why is Java giving an error after requiring an object of "IndexFile" and receiving an object of "shapefile.IndexFile"? Aren't these objects the same type?
Why is my IndexFileReader class returning an object of type "shapefile.IndexFile" instead of an object of type "IndexFile"?
Is there anything with how I have my files/folders laid out that will cause problems with compiling?
What command to I provide Windows to correctly use javac to compile my package and to compile my ReadFiles class?
I have looked through the other Incompatible Types questions and I have not found one that had led me in the right direction.
Currently trying to work with objects in Java. Everything goes fine until I hit compile. Have been reading a couple of other questions regarding the same problem, or the same given error, and at this point I am not sure wether I am forgetting something or that I need to change my classpath.
Main Class file:
package TesterClass;
public class Tester {
public static void main(String[] args){
TesterClass firstTest = new TesterClass();
firstTest.stringPrinter();
}
}
The file that is supposed to be functioning as a package file:
package TesterClass;
public class TesterClass{
private String workingSegment;
public TesterClass(){
workingSegment = "Working";
}
public void stringPrinter(){
System.out.println(workingSegment);
}
}
The 2 files are in the same directory and I am trying to manually compile them with
"javac Tester.java". The error I get is about the fact that its having issues with the package. All help is welcome!
EDIT: Forgot to post the actual compiler error.
Tester.java:9: cannot find symbol
symbol : class TesterClass
location: class TesterClass.Tester
TesterClass firstTest;
^
Tester.java:11: cannot find symbol
symbol : class TesterClass
location: class TesterClass.Tester
firstTest = new TesterClass();
^
2 errors
Move to the top of the source tree and compile both class...
So, assuming you source files are in \Java\TesterClass, you need to start in \Java
javac TesterClass\Tester.java TesterClass\TesterClass.java
You may also want to have a quick read of Code Conventions for the Java Programming Language as packages names are suppose to be in lower case :P
Updated
I just tried...
javac TesterClass\Tester.java
And it worked fine.
Are you sure that the Tester.java and TesterClass.java are in the TesterClass directory?
Updated with running example
So, basically, I dropped you .java files into the directory \compile under the TesterClass (\compile\TesterClass) directory and compiled them using...
\compile>javac TesterClass\Tester.java
Then I run them...
\compile>java TesterClass.Tester
Working
You need to go to the top of the directory hierarchy and first compile your TesterClass and then compile your Tester. Since you have not compiled your TesterClass yet, Tester is unable to find it.
The error clearly states that its not able to find the symbol TesterClass, and the reason being TesterClass hasn't been compiled yet.
I suggest you use an IDE which does the compilation automatically for you. If you stick to manual compilation, you need to compile all the classes in the proper order.
Try changing the package name so it does not match the class name. Right now they are the same. Make it package TesterClassPackage, then import TesterClass into the file with the main() method. Even though they are in the same package sometimes you need to literally import files even though they are in the same package.
javac TesterClass\TesterClass.java TesterClass\Tester.java
will do it
I have created a package:
path : /home/myid/py_ejb
File : XmppMnager.java
package xmpp;
import org.jivesoftware.smack.Chat;
public class XmppManager {
}
Compiled with
javac -d . -classpath .:smack.jar XmppManager.java
File: XmppTest.java
import xmpp.*;
public class XmppTest {
public static void main(String[] args) throws Exception {
String username = "testuser1";
String password = "testuser1pass";
XmppManager xmppManager = new XmppManager("myserver", 5222);
..}
Compiled with
$ javac -classpath .:smack.jar:./xmpp XmppTest.java
XmppTest.java:10: cannot access XmppManager
bad class file: RegularFileObject[./xmpp/XmppManager.class]
class file contains wrong class: xmpp.XmppManager
Please remove or make sure it appears in the correct subdirectory of the classpath.
XmppManager xmppManager = new XmppManager("myserver", 5222);
^
1 error
I tried a lot of way to fix this compilation issue but it just does not go away
Move the source files into a folder named xmpp so that the package names match that of the folder
Package names are directly related to the classpath directory structure. All the classes in the xmpp package need to be in a folder named xmpp, and this folder must be on the classpath. Similarly, if you had a package called xmpp.util.io you would have to put the files in xmpp/util/io/.
The usual convention is to make a src directory to hold all your source files, and then that can be filled with a directory structure that exactly matches your package structure. A pretty decent tutorial on packages can be found here.
Also, it looks like this is probably just a typo in the question, but if your file is actually named XmppMnager.java rather than XmppManager.java, that won't compile either.