How to access other files in any editor? - java

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.

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: could not find or load main class test

I have two .java files in a directory. One of them is an implementation of the Vector data structure, the other is a test class that contains a main method to test my Vector implementation. I'm trying to compile both files at the same time, so I did javac -cp . *.java, then when I run java test, I get Error: Could not find or load main class test, even though after compiling, I now have two .java files and two .class files. What do I do in order to run test from terminal?
Following is my test.java file:
package mStructures;
//import java.util.Vector;
public class test {
public static void main(String argv[]){
Vector<Integer> testV = new Vector<Integer>();
for(int i = 0; i < 39; i++){
System.out.printf("Capacity before adding %d: %d%n", i, testV.capacity());
testV.add(i);
System.out.printf("Capacity after adding %d: %d%n", i, testV.capacity());
}
}
}
For simple things it's best to have Test.java (no package, note the import)
import mStruct.Vector;
public class Test { ... main ... }
in some directory and
package mStruct;
public class Vector { ... }
in its subdirectory mStruct.
Compile and exec:
javac Test.java mStruct/*.java
java Test
First Thing - Your compilation command should have javac instead of java.
Secondly - include the same cp parameter as you have used while compiling the classes.

Setup simple java package

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.

Opening class in package from console in Java

So when i try to open a java class that's not in a package from the command prompt it all works fine, but when I try to open a class that's in a package it gives me NoClassDefFoundError.
And when I list the package when I try to open the class (java somepackage/someclass) it says that it can't load or find the main class.
Any help?
What I can infer is, you have two classes:
Test.java:
// no package defined here
class Test{
public static void main(String[] args){
System.out.println("Test");
}
}
so you can compile and run it using:
javac Test.java
java Test
Another class:
Test.java:
package test; // package defined here
class Test{
public static void main(String[] args){
System.out.println("Test");
}
}
And thus doing same thing gives you error.
For this you need to be in the parent directory of 'test' folder in your terminal or cmd and use:
java test.Test
No problem with compiler. You can compile as usual using javac Test.java from inside 'test' folder.
NoClassDefFoundError means that your JVM can't find your class at runtime. This could be because it's not visible (set to private or protected, or just no access modifier).
It could also be missing from your build path
package pkg1;
public class Dataguise{
public static void main(String [] args){
System.out.println("My name is Maninder Singh");
}
}
Suppose this is my code. My package name is pkg1.
1. First you need to create pkg1 dirrectory if not existed.
2. Run "javac Dataguise.java" command
3. It will generate the "Dataguise.class" file and move this file to "pkg1" folder
4. Now run "pkg1.Dataguise" command it will work.
I was having the same issue so sharing my experience.
What I assume is, you are creating a package and a class inside it, for example.
package com.vishwa.hello.commandLineArgs;
public class ComandLineArguments {
public static void main(String[] args) {
// TODO Auto-generated method stub
for (int i = 0; i < args.length; i++) {
System.out.println("Command line arg is "+args[i]);
}
}
}
When I try to compile and run the above code in command prompt (from the package folder) as
javac ComandLineArguments.java.
java ComandLineArguments 123 456.
You get the below error:
Error: Could not find or load main class ComandLineArguments
Caused by: java.lang.NoClassDefFoundError: com/vishwa/hello/commandLineArgs/ComandLineArguments (wrong name: ComandLineArguments)
There are 2 ways to solve this:
Go to the root path of your project and then run.
java com/vishwa/hello/commandLineArgs/ComandLineArguments 123 456
789.
Command line arg is 123.
Command line arg is 456.
Command line arg is 789.
If you want to run the program from the package directory, you need to specify the class path along with the complete package
reference.
java -cp
/Users/16399/Documents/workspace-spring-tool-suite-4-4.12.1.RELEASE/CoreJava/src/
com/vishwa/ hello/commandLineArgs/ComandLineArguments 123 456 789.
Command line arg is 123. Command line arg is 456. Command line arg is 789.

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