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).
Related
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.
I'm a relative Java newbie so apologies if the question appears somewhat basic. I've googled high and low for an answer here and I'm not finding anything that's helping.
Problem:
Whilst I'm able to integrate external packages into my Java programs from an IDE environment, I am trying to do run a very basic program from the command line that calls on a separate, basic package file that I have written - and am simply doing all this as I want to have a bottom-up understanding of how package files are related to a main program by Java.
I have a program that sits on my desktop named MyProgram.java:
import org.somepackage;
public class MyProgram {
public static void main(String arguments[]) {
System.out.println("Programme up and running...");
Human myHuman = new Human();
myHuman.scream();
}
Still on the Desktop, I then have another folder which I've named src, inside of which I have created the necessary subfolders corresponding to the package name, i.e. ./src/org/somepackage - and in this location, I have the Human.java file which defines the Human class with the following contents:
package org.somepackage;
public class Human {
public void scream() {
System.out.println("I exist!!");
}
}
I then created a classes folder, again on the Desktop, and ran the following compile command on the command line:
javac -d ./classes/ ./src/org/packagename/Human.java
This ran fine and created - as expected - the Human.class file within the ./classes/org/packagename/ location.
However, where I fall down is when I then try to compile MyProgram.java on the command line, i.e.
javac -cp ".:./classes/" MyProgram.java
As you'll see, my class path contains a reference to the current location (".") for the MyProgram.java file, and it contains a reference to the classes folder ("./classes/") which is the base location for the org.somepackage package inside whose subfolders (./classes/org/somepackage/) on can find the Human.class file.
At this stage, I was simply expecting the java engine to compile MyProgram.java into the program MyProgram.class - but, instead, I get an error:
MyProgram.java:1: error: package org does not exist
I've been following the instructions listed here:
https://www3.ntu.edu.sg/home/ehchua/programming/java/J9c_PackageClasspath.html
and I don't appear to be deviating from the instructions - yet I'm unable to locate an explanation on Stackoverflow or anywhere else as to a possible reason for this compile failure. If anyone has an idea, your help would be very much appreciated.
Thanks,
Your mistake is here
import org.somepackage; <--
public class MyProgram {
public static void main(String arguments[]) {
System.out.println("Programme up and running...");
Human myHuman = new Human();
myHuman.scream();
}
you forgot to import class actually, you need to write this name
import org.somepackage.Human; import all package content import org.somepackage.*; or write full qualified name of class in your code
org.somepackage.Human myHuman = new org.somepackage.Human();
myHuman.scream();
correct mistake:
import org.somepackage.Human;
public class MyProgram {
public static void main(String arguments[]) {
System.out.println("Programme up and running...");
Human myHuman = new Human();
myHuman.scream();
}
after that compile your Human.java by this command:
javac -d classes Human.java
and MyProgram.java
javac -d classes -cp "classes" MyProgram.java
and run MyProgram by
java -cp "classes" MyProgram
Here's my Situation:
I have a folder Structure:
C:\Users\user\Desktop\JavaTraining\Chapter3\examples.
examples is also a folder. Now, I have a file name Calculator.Java in Chapter3 folder with the package statement package Chapter3;. So, from the Command Line I compiled the file from the JavaTraining directory as javac Chapter3\Calculator.java, It compiled and I see a file Calculator.class generated. But when I run the command java Chapter3.Calculator from the JavaTraining Directory. It throwed me an error: Could not find file or load main class Chapter3.Calculator.
Then, I created a sub folder in Chapter3 named examples and copied the Calculator.java into the examples folder and tried compiling and executing the file thinking Chapter3 as the root folder ( executed commands from the Chapter3 directory). No error, the file got executed.
Please can anyone explain me why this happened or what is the reason behind it, I am going mad...
Calculator.java is just a class Calculator with main function trying to find a sum from a printsum function of two variables.
I went through the suggestions provided in http://stackoverflow.com/questions/18093928/what-does-could-not-find-or-load-main-class-mean
According to the above, it was either the syntax mistake ( the way of trying to executing the file) or setting the PATH and CLASSPATH environment variables.
I even tried the echo %CLASSPATH% to see if my CLASSPATH variable is set to current directory. It did show me the . when I executed the echo command from JavaTraining directory.
The file did not execute when I tried Chapter3 folder as root directory, but when I create a subfolder in Chapter3 and made Chapter3 as the root directory, it executed, what might be the reason or what am I doing wrong,
Here is the command line with the output:
C:\Users\vikas\Desktop\JavaTraining>javac Chapter3\Calculator.java
C:\Users\vikas\Desktop\JavaTraining>java Chapter3.Calculator
Error: Could not find or load main class Chapter3.Calculator
C:\Users\vikas\Desktop\JavaTraining>cd Chapter3
C:\Users\vikas\Desktop\JavaTraining\Chapter3>javac examples\Calculator.java
C:\Users\vikas\Desktop\JavaTraining\Chapter3>java examples.Calculator
The sum is 30
C:\Users\vikas\Desktop\JavaTraining\Chapter3>
The Calculator.java file:
// One Package Statement
package chapter3;
// The file in Chapter 3 folder, file in example folder has
//package examples;
// One or more import statements
import java.io.*;
import java.util.*;
// Class Declaration
public class Calculator {
// State. Variables and Constants
int i=10;
long k = 20;
// Behavior, one or more methods
void printSum(){
long sum;
sum = i+ k;
System.out.println("The sum is " + (i+k));
}
public static void main (String[] args) {
Calculator c = new Calculator();
c.printSum();
}
}
When you build a file, it is good to have a build directory, then java will put the class in the correct package layout.
mkdir build
javac -d build path/to/source/Files.java
java -cp build package.name.Files
My directory structure looks like this:
folder
└───subfolder
└───subsubfolder
I hava a Main.java in folder and Main.java uses class inside subsubfolder.
Here is how I did:
import subfolder.*;
import subfolder.subsubfolder.*;
However, I got the message following when I execute javac Main.java
$ javac -g Main.java
Main.java:23: error: cannot access Node
Node root = new Node();
^
bad class file: ./subfolder/subsubfolder/Node.class
class file contains wrong class: subsubfolder.Node
Please remove or make sure it appears in the correct subdirectory of the classpath.
1 error
Is my way of importing class file wrong?
It says package subfolder
The package declaration of Node should say
package subfolder.subsubfolder;
Providing an example for clarity:
folder/
Your source root (typically called 'src')
folder/Main.java
class Main { ... } (no package declaration)
folder/subfolder
folder/subfolder/subsubfolder/Node.java
package subfolder.subsubfolder;
public class Node { ... }
If your Main indeed lives in a package (i.e. if your situation is something like src/folder/Main.java) then you should not do
cd src/folder
javac Main.java
you should do
cd src
javac folder/Main.java
Your Node class declares that it belongs to package subsubfolder, but it should belong to package subfolder.subsubfolder. Alternatively, you could move directory subfolder/subsubfolder to be a sibling of directory subfolder.
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.