Packages and Classpath - java

I have created a package in the following way, and stored the Simple1.java file in d:\p\javas
Simple1.java
package mypack;
public class Simple1
{
public static void display()
{
System.out.println("Welcome to package");
}
}
i compiled it using -d switch:
d:\p\javas> javac -d d:\p\notes Simple1.java
and i got the mypack package under the P\Notes folder in D Drive.
I want to set a permanent classpath for this package so that i can access it from anywhere in my system, and i tried doing so using the environment variables.
Now, when i try importing this package in another file which is stored at another location in my system, using:
import mypack.*;
and try accessing the Simple1.class file, i get error.
the code for new java file is:
packDemo.java
import mypack.*;
class packDemo
{
public static void main(String arg[])
{
Simple1 s= new Simple1();
s.display();
}
}
Could you please help me resolving the issue, and kindly let me know the exact way of setting the classpath also.
I want to set permanent classpath for both the locations i am using to store my .java files.

1.Simple1.class file is stored in below directory upto this is fine
d:\p\notes
2.set the classpath in environment variable as follows
variable:ClASSPATH
value:d:\p\notes
3.Now navigate to packDemo.java folder compile the packDemo.java using below switch
javac packDemo.java
4.run the .class file
java packDemo
This will work fine without any problems

Related

Unable to run java with command line arguments

I have been trying to run Java with command line arguments, but for some reason the class can not be found. I am very certain the directory is correct. Is there any way to fix this?
CLDemo.java file
public class Demo {
public static void main(String[] args) {
System.out.print("It works!!!");
}
}
You need to do cd out\production before java CLDemo.
The default compile output of IntelliJ IDEA is under out\production folder, and Java needs to run at the corresponding package (folder) of your compile output.

Java ImageIO resource path

I have a Java program that runs fine in Eclipse - when I export an executable jar file it gives me an exception.
I know it is because I am giving it the wrong path but I do not know how to fix. I tried using getClass().getResourceaAsStream() (or sth. like that) but that did not work as well.
The code I am using at the moment is the following:
line 31:
BufferedImage image = ImageIO.read(new File("/src/res/loading.png"));
Okay, so I created a simple project, made a directory called res and threw some images into it
Before anyone points out that this not Eclipse, it shouldn't matter, we've already established that the Jar file contains the res directory and images, it's about replicating the structure
I then wrote a really simple example...
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
System.out.println(Test.class.getResource("/res/0.jpg"));
System.out.println(getClass().getResource("/res/0.jpg"));
}
}
which outputs...
file:/.../Test/build/classes/res/0.jpg
file:/.../Test/build/classes/res/0.jpg
No, before any points out the IDE's environment is different from the command lines, I also ran it from the command line...
java -jar Test.jar
jar:file:/.../Test/dist/Test.jar!/res/0.jpg
jar:file:/.../Test/dist/Test.jar!/res/0.jpg

Linux using a method from a jar file (Java Code)

I am trying to write a program that prints out a statement through Linux. The catch is that most of the statement must come from a method from a jar file. Basically. my code looks like this
public class identification {
public static void main(String[] args){ // Main method with print statement
System.out.print("I am " + person());
}
}
The person() method comes from the jar file. It should give something like "I am bob on a computer," but I can't quite figure out how to set up the CLASSPATH environment variable in the .bash_profile so it reads the method from the jar file. Any ideas? I am sure this is a simple task.
Thank you for your time.
use cp command to add a jar file
java -cp lib/myJar.jar myPackage.Program
also I believe you may need to import class where method person is in. I don't know how your hierarchy is so I can't help you with that.
You can add a jar to your classpath like this:
export CLASSPATH=$CLASSPATH:/path/to/your/myJar.jar

Class not found error on Jpype

I have read and searched all stack overflow .. I also found JPype class not found but it didn't help me although it is solved! I have the same problem ! I am using Mac , python 2.7.6
My both python code and A.java are on desktop. But I keep receiving this error :
Traceback (most recent call last): File
"/Users/jeren/Desktop/aa.py", line 13, in
A = jpype.JClass("A") File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/jpype/_jclass.py",
line 54, in JClass
raise _RUNTIMEEXCEPTION.PYEXC("Class %s not found" % name) java.lang.ExceptionPyRaisable: java.lang.Exception: Class A not found
aa.py :
import jpype
import os
jpype.startJVM(jpype.getDefaultJVMPath(), "-ea", "-Djava.class.path=/Users/jeren/Desktop/")
A = jpype.JClass("A")
a = A()
jpype.shutdownJVM()
A.java :
class A
{
public A()
{
super();
}
public String sayHi()
{
return("Hello");
}
public static void main(String[] argv)
{
System.out.println ("Hello ");
}
public static int add(int a, int b)
{
return(a+b);
}
}
My mac , java and python are all 64bit ! where the problem can be?
everything was ok just needed to add a 'public' to the beginning of class A:
public class A
{
public A()
{
super();
}
public String sayHi()
{
return("Hello");
}
Here are some further nodes on specifying the class path for jpype.
A. Check JDK path
I had several versions of Java JDK installed and getDefaultJVMPath did not yield the expected path. I needed to replace
jpype.getDefaultJVMPath()
with the path to the JDK, that actually has been used to compile the code, e.g
D:/jdk11/bin/server/jvm.dll
B. relative paths
It is possible to use relative paths. If my python file is for example in a package folder "pkg" and my java class file is in a sub folder "foo" of a "bin" folder:
parentFolder
pkg/main.py
bin/foo/Foo.class
jpype.startJVM(jvmPath, '-Djava.class.path=../bin")
link = jpype.JClass('foo.Foo')
For this example, the working directory of the java application will be the pkg folder. With other words, inside a main method of Foo class, you might want to use "../" to access the parentFolder.
C. -cp option does not work
I tried to use -cp option instead of -Djava.class.path, which I would found more induitive. However, the following code does not work:
jpype.startJVM(jvmPath, '-cp', classPath)
D. jars need to be included individually
I tried to include a folder with several jar files.
parentFolder
foo/main.py
lib/foo.jar
Following code does not work:
jpype.startJVM(jvmPath, '-Djava.class.path=../lib/*")
link = jpype.JClass('foo.Foo')
Each jar file needs to be included individually, e.g.:
libOath = '../lib'
libJarPaths = str.join(';', [libPath + '/' + name for name in os.listdir(libPath)])
jpype.startJVM(jvmPath, '-Djava.class.path=../lib/*")
link = jpype.JClass('foo.Foo')
(Solution from JPype (Python): importing folder of jar's )

Java Command Line Trouble with Reading a Class from a Jar Archive

I am trying to run a java based tool using a command line syntax as the following: java -cp archive.jar archiveFolder.theMainClassName.Although the class I am searching for, a main class, "theMainClassName" is in the archive.jar and in the archiveFolder given at input, I keep getting the error that my class is not seen. Does anybody have any ideas concerning this problem? Thank you in advance
Here's a concrete example of what does work, so you can compare your own situation.
Take this code and put it anywhere, in a file called MainClass.java. (I've assumed a directory called src later. Normally you'd arrange the source to match the package, of course.)
package archiveFolder;
public class MainClass
{
public static void main(String[] args)
{
System.out.println("I'm MainClass");
}
}
Then run each of these commands:
# Compile the source
javac -d . src/MainClass.java
# Build the jar file
jar cf archive.jar archiveFolder
# Remove the unpackaged binary, to prove it's not being used
rm -rf archiveFolder # Or rmdir /s /q archiveFolder on Windows
# Execute the class
java -cp archive.jar achiveFolder.MainClass
The result:
I'm MainClass
How are you building your jar file? Is the code in the appropriate package?
Does theMainClassName class have the following package line at the top:
package archiveFolder
You need the class file to be in the same directory structure as the declared package. So if you had something like:
org/jc/tests/TestClass.class
its source file would have to look like this:
package org.jc.tests;
public class TestClass {
public static void main(String[] args) {
System.out.printf("This is a test class!\n");
}
}
Then you could use the following to create the jar file and run it from the command line (assuming the current directory is at the top level, just above org):
$ jar -cf testJar.jar org/jc/tests/*.class
$ java -cp testJar.jar org.jc.tests.TestClass
Perhaps with java -jar archive.jar?
Of course, it supposes the manifest points to the right class...
You should give the exact message you got, it might shed more light.
EDIT: See Working with Manifest Files: The Basics for information on setting the application entry point (Main class) in your jar manifest file.
Usually this happens when a dependent class (static member) is not found - like this, using log4j:
public class MyClass {
private static Logger log = Logger.getLogger("com.example");
}
The reason is that the initialization of such a static member can be understood as part of the class loading - errors causing the class not to be available (loadable), resulting in the error you described.
Static constructors are another possible reason:
public class MyClass {
static {
// <b>any</b> error caused here will cause the class to
// not be loaded. Demonstrating with stupid typecast.
Object o = new String();
Integer i = (Integer) o;
}
}
I think others have covered some common stuff here. I'd jar tf the jar and make sure the class is listed. I'd also double-check that the class is public and the method is "public static void main(String[] arg)".

Categories

Resources