Able to compile java files, unable to run files [duplicate] - java

This question already has answers here:
Error: Could not find or load main class [duplicate]
(22 answers)
Closed 5 years ago.
I am trying to run these programs, but I am getting
Error: "Could not find or load main class"
Here's a screenshot of me trying to run the programs in cmd line:
Windows Powershell Screenshot:
This makes no sense to me seeing as how the files compiled just fine, which would imply that the main class was able to be found.
If anyone could explain what's going wrong I would appreciate it very much, thank you.
The UDPServer code:
The UDPClient code:

You have defined a package serverClient; at the top of both files.
So you should be having a directory named serverClient with your .class files.
if you wish to execute using java command line, you should execute from the src directory like this
PS ...\Programming Assignments\src > java serverClient.UDPServer
PS ...\Programming Assignments\src > java serverClient.UDPClient

It would be useful if you post your code here. But if I try a shot in the dark I would say that your filename doesn't match the classname in your Java file.

When running javac you pass in the paths to files you want to compile - hence those files are implicitly on the classpath. When running java you aren't passing in any files explicitly, so you have to include the current directory in the classpath in order for the JVM to know to look there.
$ javac Foo.java
$ java -cp . Foo
I use this Bash function pretty often for quick JVM/JDK experiments, if you want to try to replicate it in Powershell.
It's one of those cases where the current working directory probably should be on the classpath by default ~90% of the time, but it can't be in order to accommodate that last 10%. (Whether that's a good design decision or not is debatable, of course).

You are:
In the wrong directory. You should be in the directory that contains serverClient.
Using the wrong commands. They should be:
javac serverClient/*.java
java serverClient.UDPServer
java serverClient.UDPClient

Related

How to run a java class file in windows 10 [duplicate]

This question already has answers here:
How do I run Java .class files?
(4 answers)
Closed 4 years ago.
I crawled through the forums and tried a few things, I am just trying to run a java file I downloaded from github that I did not make. I compiled it fine, but am now stuck. Here's what I've done so far (In the downloads directory for both):
java.Randomizer,
and
java -cp C:\Users\enajo\Downloads\Randomizer
Both resulted in Error: Could not find or load main class
The file is located in downloads and is named Randomizer.class with the classname being ca.pogo4545.randomizer. What am i doing wrong?
If you declared a package name (that you mentioned as classname) the run command line must include the name of it:
java ca.pogo4545.randomizer.Randomizer
But in this case the file also must be in a directory structure that represent the same package, and if you using more than one class you must include the -cp arguments
First of all the name of the class must be the same as the name of the file .
Java and Javac are case-sensitive.
Compile like : javac HelloWorldApp.java
And run like : java -cp . HelloWorldApp

Error: Could not find or load main class HelloWorldApp

I am pretty new to this but I seem to have a problem for a day now, and I can't get around it. Have looked over the other similar post but nothing worked. I just got the basic tutorial, made my Java file.
Used the javac HelloWorldApp.java command and got the class file.
But every time I try to run the java -cp . HelloWorldApp command, I get the Error: Could not find or load main class HelloWorldApp error. Pretty sure I am doing something wrong, just don't know what. I am on a Windows 7 machine with jdk1.8.0_31.
You can simply run your .class file with java filename command, provide you are running it from the src directory
Your current working directory may not be in your classpath, before running java class issue this command to include PWD into your classpath :
set CLASSPATH=%CLASSPATH%;.
java yourAppName

Java code not working in OSX Mavericks

I'm trying to run a java library, so that I can build upon it and do my customization. The library is called jayu parses ASN files so that you can decode them. It can be downloaded here
There are a few test data to check the library in the "test" folder and mentioned in the Readme.txt file. There is a asn2csv batch file for windows but I'm using OSX mavericks. According to the Readme file, I need to run it by invoking the command:
ls $ASN_DATA_DIR/*.dat | xargs java -cp "./*.jar:." Path/To/Stream1.txt test.testdata.Stream1 $OUTPUT_DIR
OR
java -cp "*.jar;$PATH_TO_TEST_DIR" Stream1.txt test.testdata.Stream1 . Path/To/Stream1.dat
But whatever I try I always get Error: Could not find or load main class Stream1.txt
I'm not a Java programmer. What am I doing wrong here? The development of this seems to be inactive lately but it's still useful. So, I'm posting it here, hoping someone will help me run the example.
Edit: I've added the tree structure of the directory and the files
+ jayu
|--Readme.txt
|--commons-compiler.jar
|--janino.jar
|--jayu.jar (ASN parser)
|--AsnToCsv.bat (Command line Tool)
+--test (Contains test data for examples)
|
+ testdata
|
Stream1.txt (Grammar File)
Stream1.dat (ASN Data File)
Stream1.java {mapFile}
...
It is interpreting Stream1.txt as the Java class you are trying to execute, because it is treating at as your first argument to java. Your first argument should be the name of the class containing the main(), or, the executable JAR must be indicated with a -jar option.
This would seem to indicate that "*.jar;$PATH_TO_TEST_DIR" is evaluating to blank. Can you see if there are any .jar files in your current working directory? Also what is the value of $PATH_TO_TEST_DIR?
Another thing is that if you use the second form on OS X, you should have a : instead of a ; because it is a Unix-based OS, not Windows.
Update1
I had a slight error with my description of how to run an executable JAR. You use the -jar option, not -cp (I corrected it above). Since they put -cp in their invocation, I'm guessing they are not intending to target the executable JAR, but rather to name the main class. That to me says that test.testdata.Stream1 is that main class, which means the ordering they gave you is wrong. Try this:
java -cp "*.jar:$PATH_TO_TEST_DIR" test.testdata.Stream1 Stream1.txt . Path/To/Stream1.dat
or some other ordering that starts with:
java -cp "*.jar:$PATH_TO_TEST_DIR" test.testdata.Stream1 ..........
That is, that makes test.testdata.Stream1 the very first command line argument to java.

Once again: "javac: file not found: hello.java [duplicate]

This question already has answers here:
Why this javac: File Not Found error?
(5 answers)
Closed 8 years ago.
I was using Java SE Jdk1.7.0_40 and it was working perfectly fine. I've no idea what went wrong, I've set the path many times through different ways. If I check in cmd like by writing javac or java -version, it results affirmative but when i write
javac hello.java i get this error..
javac: file not found: hello.java
Usage: javac
use -help for a list of possible options
I went through almost all of topics related to my problem but all are saying the same thing that is "to set the path, may be your path is not correct, etc etc". I've re-install java but still no use.
Is there any step by step procedure to check whether the path is actually responding or not?
Am using Windows 7 and current version of Java is SE jdk1.7.0_60
a thing more.. when I compile the java file from the folder containing that file, it works fine and generate .class file but when I execute the class file ..
java hello , i get this
Error: Could not find or load main class hello
may be i'm missing something..
Help...
There is no problem with javac not being on your PATH, since the error you see is an error from javac, which could only occur if javacis on your path and running.
The only possibility is that hello.java is not in the current working directory from which you are invoking the command. Putting the directory that hello.java lives in onto your PATH will not help here.

"Could not find or load main class" Error while running java program using cmd prompt [duplicate]

This question already has answers here:
What does "Could not find or load main class" mean?
(61 answers)
Closed 2 years ago.
I am running a simple "HelloWorld" Program. I get this error in the command prompt:
Could not find or load main class HelloWorld.
I have set the CLASSPATH and PATH variable in the system. In the cmd prompt, I am running from the directory where I have saved HelloWorld program. I can see the class name and the file name are same and also .class file created in the same directory. What else could be the problem?
My sample program looks like this:
package org.tij.exercises;
public class HelloWorld {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Hello World!!");
}
}
When the Main class is inside a package then you need to run it as follows :
java <packageName>.<MainClassName>
In your case you should run the program as follows :
java org.tij.exercises.HelloWorld
What's your CLASSPATH value?
It may look like this:
.;%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\lib\tools.jar
I guess your value does not contain this .;.
So, ADD IT .
When you done , restart CMD
That may works.
For example the file HelloWorld.java is in path: D:\myjavatest\org\yz\test and its package is: org.yz.test.
Now, you're in path D:\myjavatest\ on the CMD line.
Type this to compile it:
javac org/yz/test/HelloWorld.java
Then, type this to run it:
java org.yz.test.HelloWorld
You may get what you want.
I removed bin from the CLASSPATH. I found out that I was executing the java command from the directory where the HelloWorld.java is located, i.e.:
C:\Users\xyz\Documents\Java\javastudy\src\org\tij\exercises>java HelloWorld
So I moved back to the main directory and executed:
java org.tij.exercises.HelloWorld
and it worked, i.e.:
C:\Users\xyz\Documents\Java\javastudy\src>java org.tij.exercises.HelloWorld
Hello World!!
Since you're running it from command prompt, you need to make sure your classpath is correct. If you set it already, you need to restart your terminal to re-load your system variables.
If -classpath and -cp are not used and CLASSPATH is not set, the current directory is used (.), however when running .class files, you need to be in the folder which consist Java package name folders.
So having the .class file in ./target/classes/com/foo/app/App.class, you've the following possibilities:
java -cp target/classes com.foo.app.App
CLASSPATH=target/classes java com.foo.app.App
cd target/classes && java com.foo.app.App
You can check your classpath, by printing CLASSPATH variable:
Linux: echo $CLASSPATH
Windows: echo %CLASSPATH%
which has entries separated by :.
See also: How do I run Java .class files?
I had the same problem, mine was a little different though I did not have a package name. My problem was the Class Path for example:
C:\Java Example>java -cp . HelloWorld
The -cp option for Java and from what I can tell from my experience (not much) but I encountered the error about 20 times trying different methods and until I declared the class Path I was receiving the same error. Vishrant was correct in stating that . represents current directory.
If you need more information about the java options enter java -? or java -help I think the options are not optional.
I just did some more research I found a website that goes into detail about CLASSPATH. The CLASSPATH must be set as an environment variable; to the current directory <.>. You can set it from the command line in windows:
// Set CLASSPATH to the current directory '.'
prompt> set CLASSPATH=.
When you add a new environment setting you need to reboot before enabling the variable. But from the command prompt you can set it. It also can be set like I mentioned at the beginning. For more info, and if your using a different OS, check: Environment Variables.
One reason for this error might be
Could not find or load main class <class name>
Maybe you use your class name as different name and save the class name with another name you can save a java source file name by another name than class name. For example:
class A{
public static void main(String args[]) {
System.out.println("Hello world");
}
}
you can save as Hello.java but,
To Compile : javac Hello.java
This will auto generate A.class file at same location.
Now To Run : java A
Execute your Java program using java -d . HelloWorld command.
This command works when you have declared package.
. represent current directory/.
I had a similar problem when running java on win10
instead of
$ java ./hello
Error: Could not find or load main class ..hello
Run
$ java hello
Hello, World
I was getting the exact same error for forgetting to remove the .class extension when running the JAVA class. So instead of this:
java myClass.class
One should do this:
java myClass
I used IntelliJ to create my .jar, which included some unpacked jars from my libraries. One of these other jars had some signed stuff in the MANIFEST which prevented the .jar from being loaded. No warnings, or anything, just didn't work. Could not find or load main class
Removing the unpacked jar which contained the manifest fixed it.
I faced the same problem and tried everything mentioned here.
The thing was I didn't refresh my project in eclipse after class creation .
And once I refreshed it things worked as expected.
faced the same problem. solved by following these steps
go to directory containing the package 'org.tij.exercises' (e.g: in eclipse it may be your src folder)
use java org.tij.exercises.HelloWorld
For a lot of us, at least for me, I think the class path hierarchy is not intuitive since I'm working inside a directory structure and it feels like that ought to be it.
Java is looking at the name of the class based on it's package path, not just the file path.
It doesn't matter if:
i'm in the local directory ./packagefoo/MainClass, or
a directory up ./packagefoo/, or
one down ./packagefoo/MainClass/foo.
The command "java packagefoo.MainClass" is running off the root %CLASSPATH% which means something significant to Java. Then from there it traverses package names, not path names like us lay coders would expect.
So if my CLASSPATH is set to %CWD%/, then "java packagefoo.MainClass" will work.
If I set the CLASSPATH to %CWD%/packagefoo/ then packagefoo.MainClass can't be found.
Always "java MainClass" means nothing, if it is a member of "package", until I rip out the java code "package packagefoo;" and move the Class File up a directory.
In fact if I change "package packagefoo;" to "package foopackage;" I have to create a subfolder under CLASSPATH/foopackage or foopackage.MainClass stops working again.
To make matters worse, Between PATH, CLASSPATH, JAVAHOME, for Windows, JDeveloper, Oracle Database, and every user name it was installed under, I think a coder trying to just get something up fast ends up brute forcing path variables and structure until something works without understanding what it means.
at least i did.
Create a folder org/tij/exercises and then move HelloWorld.java file. Then run below command
javac -cp . org/tij/exercises/HelloWorld.java
AND
java -cp . org/tij/exercises/HelloWorld
I was facing similar issue but it was due to space character in my file directory where I kept my java class.
Scenario given below along with solution:
public class Sample{
public static void main(String[] args) {
System.out.println("Hello world, Java");
}
}
My Sample.java class was kept at Dir "D:\Java Programs\Sample.java"[NOTE: Package statement not present in java class].
In command prompt, changed directory to "D:\Java Programs\", my programmed compiled but failed to run with error "Could not find or load main class"
After all the possible solutions over SOF(nothing worked), I realized may b space causing me this issue.
Surprisingly removal of folder name space char['Java Programs' -> 'JavaPrograms'], my program executed successfully. Hope it helps

Categories

Resources