I am fairly new to Java and wrote simple a program to strip unwanted contents from a CSV export.
The top of the program looks like this:
package csv;
import java.io.*;
import java.util.*;
public class csvstrip {
public static void main(String[] args) throws Exception {
Eclipse automatically builds the csvstrip.class in a folder {workspace}\bin\csv. However, whatever I type into command line (running it using the java command), I get the following:
Error: Could not find or load main class csvstrip
If I remove the package declaration from the code, Eclipse asks if you wish to move the csvstrip class to the default package and builds the the class a level up in the folder {workspace}\bin. Now, on entering
java csvstrip
the program functions fine. Before removing the package csv declaration, I tried:
java csv.csvstrip
java csvstrip
from both the bin and bin\csv folder but nothing seemed to run. What piece of information am I missing here? If you define a package for your code, from where in command line are you supposed to run the java command and how should your program be referenced?
I presume that you are trying to run the class file from top level or default package folder.
java will obviously throw you 'Class Not Found' error, because you are looking in the wrong directory.
trying running.
java .\csv\csvstrip
in the assumption that you are in the present working directory which contains csv folder within which you have csvstrip or try giving the complete path.
In your configuration you should be able to start your class standing in the bin folder and typing
java csv.csvstrip
...or...
java csv/csvstrip
should also work just fine
If you are on a windows machine please double check the case of your class file and of your package folder. Windows is case insensitive java classloader isn't.
Related
Hello so I'm trying to Import a class from another project I made but I cant get it to work..
this is my code:
import program.GUI;
public class name {
//code
}
I get the following error
Opens the new class wizard to create the type.
package: program
public class GUI {
}
I have created a prog02>src>program>GUI.java
How can I solve it.
I think I see the problem here...
I don't know if your running java from cmd, but my explanation is going to assume you are. Outside of using IDEs like Netbeans and Eclipse that pretty much streamline the build process of java code, it's good to know how it works.
Ok, here goes:
1) Compile your java source code files into bytecode (.class files) by invoking JAVAC
javac [optional flags] [path to file intended for compilation]
This creates the bytecode that the JVM will need in order to execute.
2) Invoke the Java interpreter (JVM) to run your bytecode.
java [optional flags] [name of class file w/o .class extension]
If everything goes right this command will create a JVM process with main.java being the main entry to the program that creates the initial thread that runs your program.
Here what you should write to get you program to compile with you package dependencies.
cd to base dir that contains program with main method (src)
javac -cp . program/ (check by going to dir to see if a GUI.class file
was generated)
javac -cp . Main
java -cp . Main
That should do it. Otherwise from and IDE standpoint you either don't have file in the right directory or you are not using the right syntax for specifying your package and import.
The package declaration on the GUI class is not right. It should be:
package program;
public class GUI {
}
You may also find this useful: Java Code Conventions
I am trying to run my Example2.class program in windows 7 64-bit command prompt. I used command prompt already to compile the program, but when type: "java Example2" it gives me an error saying could not find or load main class example2. How do I set the right path to my file so that it can find it? Thanks
You need to give it the full package name, and (unless you change the class path) you need to be in the right directory. If the full package name is
com.something.Example2
then you'd expect the compiler to produce a file like this:
com/something/Example2.class
If you make sure you're in the directory immediately above com (i.e., you can see com when you do a directory listing), then you can run it with
java com.something.Example2
Note that it's case sensitive.
If you used the default package (i.e., the full class name really just is Example2) then you need to be in the directory containing Example2.class, and then you run
java Example2
But using the default package is discouraged.
The biggest thing you could do to help yourself out is to use an IDE (Eclipse or NetBeans are the most commonly used ones). As soon as you start to write anything at all large or complex, compiling and running from the command line without an IDE will cause you to claw your own eyes out.
When you write a class you can save the file as : MyClass.java and then execute this commands in this directory:
javac MyClass.java
which will compile the class and then create automatically the file: MyClass.class (If compilation ended without errors)
and then to run this SPECIFIC class execute the command:
java Myclass
I am trying to run a java program through the Terminal on Mac, yet getting:
Error: Could not find or load main class (MY CLASSNAME)
I compiled this application with Eclipse, and when I run this with Eclipse, it works fine.
Furthermore, I am in the right directory, as when I type "ls" in the Terminal, it lists all the files, includes the class file I am trying to run.
This is what I type:
java mainClass
I would very much appreciate help to solve this!
Thank you,
Dean
EDIT: Solution - instead of java mainClass, it must have package too: java startPackage.mainClass
Start by making sure you are at the directory above the top level package
If the class belongs to the package com.foo.bar, you want to be in the directory above com.
In your case, you want to be in the directory above startPack.
Then you need to use the fully qualified name to run the class...
java statPack.mainClass
For example...
Make sure you have the current directory inside your CLASSPATH.
java -cp . mainClass
To set this globally, you can use export CLASSPATH=$CLASSPATH:. inside .bash_profile.
Separately, if your class lives inside a package such as com.foo.bar, then you will need to go to the parent directory of com and run your application with the full path.
java com.foo.bar.mainClass
I too faced this on Mac machine and then what I had to do to make it work was:
Problem Statement:
I had one package xyz under the root of project i.e src/main/java and then inside xyz package I had one class Student.java
my current directory is /Users/username/projectname/src/main/java/xyz:
I can see Student.java exists here
and I compiled it using javac Student.java
Now I see class file has been created at this location. But when I try to run the class file using java Student
I get the error: Error: Could not find or load main class Student
Solution:
Now the solution is to go one step back in the directory and go to root path:/Users/username/projectname/src/main/java and run the command
java xyz.Student
and it will work.
Link to follow: https://javarevisited.blogspot.com/2015/04/error-could-not-find-or-load-main-class-helloworld-java.html
For people dumb like me, make sure you are typing java HelloWorld - and NOT java HelloWorld.class - to run the compiled file with the name HelloWorld.class. This is especially so if you are used to hitting the tab key to complete the file name, as the terminal will give you java HelloWorld.class if you hit the tab key for autocomplete after typing something like java He...
This answer is here because it took 3 sites, including this answer, and 25 mintues before I figured out what I was doing wrong.
Logic is easy, typing is hard.
Using the absolute path can also resolve this problem:
java -classpath /Users/xingliu/IdeaProjects/springproject/src/main/java/ startPackage.mainClass
I am using Notepad++ to write my Java code and Command Prompt to compile and run it.
Following is my sample Java code,
package abraKadabra;
public class SuperClass{
protected int anInstance;
public static void main(String [] abc){
System.out.println("Hello");
}
}
However, this file is in the following folder structure :
"usingprotected\superPkg" (usingProtected is a folder somewhere in the hierarchy in C:)
So, my package name here should be something like usingProtected.superPkg instead of abraKadabra as I wrote it.
But, when I compile this Java code from command prompt, it compiles fine with no error or warnings. Why is it so? Shouldn't the package name adhere to the folder structure?
And if it should, how would it adhere?
For e.g. if my package name is usingProtected.superPkg, will the compiler check in the reverse order. The present working directory should be superPkg, then the parent directory should be usingProtected and its done. Is it how it checks the folder structure with package name?
The Java language specification doesn't force files to be in a certain directory. It optionally allows the compiler to require that public classes are in files with the same name of the class, but I don't think there's anything similar for packages. Section 7.2.1 talks about possible storage options in a file system, but it doesn't say anything about enforcing source code structure, as far as I can see.
However, it's best practice - and a pretty much universally accepted convention - to reflect the package structure in the source directory structure... and javac will use this to try to find source files which aren't explicitly specified to be compiled.
Note that if you're compiling from the command line, by default each class will appear in the same location as the corresponding source file, but if you use the "-d" option (e.g. "-d bin") the compiler will build an appropriate output directory structure for you, rooted in the specified directory.
After experimenting a bit, I got the way how to use package name and run Java class files from command prompt.
Suppose following is my Java source file:-
package mySample;
public abstract class Sample{
public static void main(String... a){
System.out.println("Hello ambiguity");
}
}
This file is in directory "D:\Code N Code\CommandLine".
Now, when compile the source code (by going to the above directory from cmd) using following command:-
javac -d . Sample.java
This automatically creates "mySample" folder in my current directory. So, my class file Sample.class is present in directory "D:\Code N Code\CommandLine\mySample". Compiler created this new folder "mySample" from the package name that I gave in my source code.
So if I had given my package name to be "package com.mySample", compiler would create two directories and place my class file in "D:\Code N Code\CommandLine\com\mySample".
Now, I am still in the present working directory i.e. in "D:\Code N Code\CommandLine". And to run my class file, I give the following command:
java mySample.Sample
So, I give the complete hierarchy of package and then the class name. The Java Interpreter will search the current directory for "mySample" directory and in that for "Sample.class". It gets it right and runs it successfully. :)
Now, when I asked that why it compiles my wrong package source code, it would compile the code successfully though, but it gives NoClassDefFoundError when I run my class file. So above method can be used to use package names from command line.
If you're compiling a single class, javac doesn't need to look elsewhere for it. It'll just compile the file as is and put the resulting .class into the same folder. However, you generally won't be able to use the class til you put it into an "abraKadabra" directory in one of the directories in the class path.
If your class uses another class in the package, though, you might have problems compiling it where it is, for the same reason (javac wants to find the class and make sure it has the methods and such that your class uses).
Java compiler does not check the directory structure when it compiles source files. As you mentioned, suppose you have a source file that starts with the directive
package abraKadabra;
You can compile the file even if it is not contained in a subdirectory .../abraKadabra . The source file will compile without errors if it doesn’t depend on other packages. However, the resulting program will not run (unless also including package name in execution). The virtual machine won’t find the resulting classes when you try to run the program.
I have a java program with multiple class files and they are all stored in the same folder called lab7. I coded the project in NetBeans so used "package lab7" in all the files. My main application java file is called lab7.java. Now, when i try to run this on the terminal i get "Exception in main thread: NoClassDefFoundError". I do the following inside the folder lab7.
javac *.java
java lab7
I don't know why get this error. It should be some basic class path error. Thanks for the help.
Normally class names should start with a capital letter. So you should rename your main class to Main. If it's inside the lab7 package, run this:
java lab7.Main
This should be run in the directory that contains the lab7 directory. So if you're in the lab7 directory itself, go up one level first.
Use
java lab7.lab7
You do have a lab7.java file with a public static void main(String[]) method, right?