Setup simple java package - java

On linux RedHat machine in /home/user/Downloads/ I do mkdir proj, cd proj, mkdir lib.
Now I want to have my java program residing in the proj package, so I have:
package proj;
import static proj.lib.Mathem.*;
public class MyJavaApp {
public static void main(String[] args) {
System.out.println("abc");
time = 2013;
}
}
and my library residing in a proj.lib subpackage, so I have:
package proj.lib;
public final class Mathem {
public static long time;
private Mathem() {}
}
if I cd lib/ and do javac *.java then Mathem.java compiles fine into Mathem.class but when I go back cd .. and do javac *.java I get an error saying that package proj.lib does not exist.
Could you please help to understand why java compiler does not see such simple package structure ?

It sounds like you are in the proj directory when it fails. You need to be in the directory above that, i.e., in /home/user/Downloads in your example. Then enter "javac proj/MyJavaApp.java"

There is something called Classpath... I suggest you read up on it

Try adding a public static long getTime() method in Mathem.

Related

How to access other files in any editor?

For example I have Main.java and test.java. test has public static int bro = 5; so i try to print test.bro from Main but the class test is not found. An IDE like Eclipse takes care of this for me but how do I do this with an editor? Sorry noob question. I'm in cmd in the directory of deez files and i type javac Main.java, den java Main. Thanks.
file Main.java:
public class Main {
public static void main(String[] args) {
System.out.println(test.bro);
}
}
file test.java:
public class test {
public static int bro = 5;
}
So suppose you have two source files: Main.java and test.java then you need to compile them first.
You can do it via command javac Main.java test.java. That command will produce 2 files in your current directory: Main.class and test.class. Which contain compiled java code.
Now you need to run your main class with classpath which contains both of your classes. So you need to run command java -cp . Main. Where . represents directory with your compiled classes.

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.

How to run a java program in command line?

Here is the thing: I am trying to run the example program in the joda-time project.
The start of the Examples.java file looks like this:
package org.joda.example.time;
import java.util.Locale;
import org.joda.time.DateTime;
import org.joda.time.Instant;
/**
* Example code demonstrating how to use Joda-Time.
*
* #author Stephen Colebourne
*/
public class Examples {
public static void main(String[] args) throws Exception {
try {
new Examples().run();
} catch (Throwable ex) {
ex.printStackTrace();
}
}
And all the classes for compiling this Example.java is in a joda-time-2.3.jar.
I can successfully compile this program by using
javac -cp somewhere/joda-time-2.3.jar Example.java
And it generate an Example.class, but I jut cannot execute that.
So far I have tried:
java Examples
java -cp somewhere/joda-time-2.3.jar Examples
java -cp somewhere/joda-time-2.3.jar org.joda.example.time.Examples
But they all generate this kind of errors:
Error: Could not find or load main class org.joda.example.time.Example
Error: Could not find or load main class Examples
And I've tried both in the org/joda/example/time folder and the parent folder of org
Anyone can give an instruction on how to execute that? Really appreciate it!
Error: Could not find or load main class org.joda.example.time.Example
public class Examples {
Name of your class is Examples not Example
EDIT
Sorry for late reply...
To execute specific Java program you need to bring control to root directory so if your class is in abc/newdir/Examples.java you need to use cd command (in windows) to lead control to root directory and than compile or you can defeneitly go for the suggestion of kogut.
C:/abc/newdir>java -cp somewhere/joda-time-2.3.jar Examples
Modify your classpath parameter, so it should include directory where Example.class was generated.
In case of out/org/joda/example/time/Example.class you need to use
java -cp somewhere/jodata-time-2.3.jar:out org.joda.example.time.Example

Packages and Classpath

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

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