I downloaded an external library, common-codecs, and am trying to create a package from the downloaded source code so that i can import and use it in java class files. How would i go about doing this?
I moved the downloaded directory into the same directory as my java class files.
What I've tried so far:
package commons-codec-1.11-src;
I place this at the head of my java class file
Then i try and compile the file using javac in the Linux terminal
javac -cp ~/Documents/javapractice/commons-codec-1.11-src ~/Documents/javapractice/File.java
I get a "interface, class, or enum required error" and the compiler error points to the package statement in the java class file.
code:
import java.util.*
package commons-codec-1.11-src;
public class File
{
........
}
Just to clear things up commons-codec-1.11-src is source code I downloaded and is now a directory in the same directory as File.java
Any help would be greatly appreciated! Thank You!
I downloaded an external library, common-codecs, and am trying to
create a package from the downloaded source code so that i can import
and use it in java class files. How would i go about doing this?
You don't need and you should not package the source code of the external library in your application.
Extracting dependency classes in your own application is a very corner use case and it should done only as you have no choice.
What you need is adding the jar that contains the compiled classes in your classpath at compilation (javac command) and at runtime (java command).
Supposing that the jar is named commons-codec-1.11.jar, to compile your File.java class you should execute :
javac -cp ~/Documents/javapractice/commons-codec-1.11.jar /~/Documents/javapractice/File.java
The File.java declaration is not correct either.
The package declaration has to happen before the import declaration and the package and import values are not correct either.
It should be something as :
package javapractice;
import java.util.*;
public class File {
........
}
About import from the third party library, you need to import classes you use in File class.
You cannot import the whole package as you try to do.
I think that you should try to understand javac/java bases and start with an IDE to make things easier.
Related
I am trying to use pdfbox to write a simple pdf file but the problem is that I am getting error :
cannot find symbol class PDDocument
I have downloaded the jar files into the same folder the program exists. How to fix this compilation error?
package org.apache.pdfbox.pdmodel.PDDocument;
import java.io.*;
import org.apache.pdfbox.pdmodel.PDDocument;
public class pdf
{
public static void main(String args[])
{
}
}
Putting the jar in the same folder or package does not add it to the class path. You need to mention the path of the jar in your class path while running your java program. Here is the syntax for that:
To compile:
javac -classpath .;yourjar.jar src/your/package/*.java
To run
java -classpath .;yourjar.jar src/your/package/yourprogrammeclassname
You will need to make sure that the JAR file is on the classpath.
having a similar issue I found that I did not have the correct syntax on the import line in the java source
doing a compile as the following (on windows):
javac -cp .;commons-io-2.4.jar AgeFileFilterTest.java
with commons-io-2.4.jar in the same folder as AgeFileFilterTest.java
I was getting error:
import org.apache.*;
^
AgeFileFilterTest.java:24: error: cannot find symbol
displayFiles(directory, new AgeFileFilter(cutoffDate));
^
It was puzzling becuase it seemed all was in place; the jar was in the folder, defined in the classpath, and upon jar content inspection I could see what was being referenced- using 7zip I opened the jar file and could see:
commons-io-2.4.jar\org\apache\commons\io\filefilter\AbstractFileFilter.class
I then read in some post "you do not import the class" which got me to think about the import syntax...
I changed it from:
import org.apache.*;
changing it to:
import org.apache.commons.io.filefilter.*;
and wala compile error gone using:
javac -cp .;commons-io-2.4.jar AgeFileFilterTest.java
and program worked using
java -cp .;commons-io-2.4.jar AgeFileFilterTest
I have a jar file named stdlib-package, the .class files in this jar file are all in a edu.princeton.cs.introcs package
For example, I want to use one class named StdDraw from that jar file in my own codes
Say E:\code is my current working directory. like other conditions, I create a subdirectory bin\edu\princeton\cs\introcs to put stdlib-package in
Here is my codes:
package com.david.test;
import edu.princeton.cs.introcs.stdlib-package.*; // any problems here ?
public class DrawTest
{
public static void main(String[] args)
{
StdDraw.line(0.5, 0.5, 1, 0.5);
}
}
Then I put DrawTest.java in the src\com\david\test
To compile the DrawTest.java, I type the commands in command line:
javac -d bin -cp bin\edu\princeton\cs\introcs\stdlib-package.jar src\com\david\test\DrawTest.java
But it failed : package edu.princeton.cs.introcs.stdlib_package doesn't exist...
I searched for a lot of time, but haven't found the answer
Thanks for your help
EDIT===
#EJP's comment solves my question too.
This should be all you need in code:
import edu.princeton.cs.introcs.*
Then you only need to make sure the jar is on your class path; it doesn't matter what the name of the jar is, or where it is. If it is on your classpath, Java will load it, and use the location of the files within it.
Move the jar into a lib directory in your current directory, and do something like
javac -cp lib\stdlib-package.jar src\com\david\test\DrawTest.javac
Your import is wrong. There's no such package in that .jar file.
import edu.princeton.cs.introcs.*;
In general, however, that's a bad practice and you should really only import what you need:
import edu.princeton.cs.introcs.StdDraw;
I understand that there is no package with 'stdlib-package' name.
If the fully qualified name of the class you are using is edu.princeton.cs.introcs.StdDraw, you can just import 'edu.princeton.cs.introcs.StdDraw'
import edu.princeton.cs.introcs.StdDraw
If you want to load all the classes in that package
import edu.princeton.cs.introcs.*
I'm a seasoned PHP programmer with backgrounds within Powershell, JavaScript etc.
I see the benefits with Java and wish to dig deeper.
All the documentation though is too advanced for me atm.
A typical PHP structure:
index.php
classes/class_get.php
classes/class_set.php
Where in index.php you would have the lines
require_once "classes/class_get.php";
require_once "classes/class_set.php";
Now in Java...
I understand Windows files are in .JAR format. No problem here, easy to understand.
I am confused when creating this in eclipse. Is the strucutre;
Main.java
src*/defaultPackage**/myClass.java
* folder
** package
Where in the Main.java file you would declare "import myClass.java"?
Also other packages such as the following:
import java.awt.*;
import java.awt.event.*;
import java.net.URL;
import javax.swing.*;
I am getting errors, and this manifest file, I haven't heard of it in any tutorials yet?
Try this, this is the way to create a jar or runnable jar in eclipse
File -> Export-> Java ->Runnbale JAR file
Launch configuration : your Class containing the public static void main(String[] args)
Export destination : Target place
Library Handling:
Package required libraries into generated JAR
FINISH
a) There are no Windows files in Java. Java is cross platform.
b) Something with a slash delimiter is always a folder. Something with a dot is always a package. Don't confuse them, because it is confusing enough.
c) Don't use the term "defaulPackage", because there is such a term for the case, that you don't specify any package. Then the package of your class is called the default package.
Main.java
src*/defaultPackage**/myClass.java
Where in the Main.java file you would declare "import myClass.java"?
You never import something .java, because you import a class, not a source file. Often you only have third party compiled classes in a jar, and don't have the source. Well - maybe.
If your class belongs to a package, the name of the class is the whole package name. You can omit it from classes in the same package.
So we don't know whether Main and MyClass (use Upper case, if you like to communicate with others - else you're confusing us) belong to the same package.
If so: Don't import anything.
Else: Import the whole package name, which might contain multiple dots.
So for example:
import yourCompany.games.monstersahead.*;
or
import yourCompany.games.monstersahead.MyClass;
for example.
The package name will usually not contain folder names like src, bin, classes.
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.
Here is my JUnit test class:
package com.bynarystudio.tests.storefront;
import java.util.List;
import org.junit.Assert;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import com.bynarystudio.tests.BaseWebTest;
public class SmokeTests extends BaseWebTest {
#Test
public void StoreFrontMegaNavTest(){
webDriver.get(default_homepage);
String source = webDriver.getPageSource();
Assert.assertTrue(source.contains("some text"));
}
}
How do I run this test from the command line? When I try to run it from inside its directory using
java -cp junit.textui.TestRunner SmokeTests.java
I get the following error
Could not find the main class: SmokeTests.java. Program will exit.
I think this has to do with the fact that my classpath is not setup properly. But I have no idea because I'm brand new to Java. Coming from .NET, C#, and Visual Studio, the whole classpath thing makes no sense. Even though I have all my files correctly added to a project in Eclipse (I know because the test runs fine from inside Eclipse), it will absolutely not run or compile from the command line.
First of all you are mixing two things:
First you have to compile the project using javac command. As a result you will get set of .class files (not .java -> this is source code)
Then you can run the code using java command:
java -cp classPath yourpackage.SmokeTests
where:
classPath - is the list of directories or jar files where your compiled classes are, if you jave multiple entries separate them using ";" (windows) or ":"(Linux)
so your classPath can be: -cp .;c:/jars/*;deps
which means your classpath will contain:
current directory
all jar files from c:/jars/*
all jar files from deps directory that is in your working dir
So the full command can will be:
java -cp .;c:/jars/*;deps SmokeTests
yourpackage - is the package of the SmokeTests class, if you do not have package defined in the SmokeTests.java leave this empty