Execution of Java class file - java

When I try to execute a java file within a package, the integrated terminal can't find the class file. How should I configure vscode or the command to automatically add the packages on the command like java package.File or search the class file automatically? I'm using Code Runner extension with this command: "java": "cd $dir && javac -d \"$workspaceRoot/java/bin/\" $fileName && cd \"$workspaceRoot/java/bin/\" && java $fileNameWithoutExt. This works when the java file isn't in a package.

You need to have java run as <package>.ClassName. For example, if your class looks like this:
package com.something;
class Something {
public static void main(String[] args) {
System.out.println("Hello World");
}
};
And you run javac with the following:
javac -d . Something.java
Then, you need to make sure that java knows which package you want to run:
java com.something.Something;
In other words, you need to explicitly tell java what package your main class is in.
Basically, you need to create another variable inside of vscode that allows you to add the package to the beginning of the command:
"java": "cd $dir && javac -d \"$workspaceRoot/java/bin/\" $fileName && cd \"$workspaceRoot/java/bin/\" && java $filePackage.$fileNameWithoutExt
Unfortunately, it doesn't look like https://github.com/formulahendry/vscode-code-runner has a java package variable already defined, so it's something you'd have to either create by downloading the source of the package and hacking it, or requesting a variable from the package author at https://github.com/formulahendry/vscode-code-runner/issues
Update:
It looks like this is already an issue in the CodeRunner package: https://github.com/formulahendry/vscode-code-runner/issues/118. You might want to watch this issue for updates and upgrade your extension when it is fixed.
As a temporary workaround, I think just not specifying a package for your main class will work. All other classes are going to be using import anyway, so it doesn't matter for those, but keep your main class in the top-level package and VSCode Code Runner should work as expected.

Related

Error: Could not find or load main class ( in Java 8)

I am trying to learn Java and I've made my first program and compiled it into a class file (the file is called aye.java and when compiled I have aye.class, I think the compilation worked). However when I use the java command in the folder where the class is located it just returns below error -
Could not find or load main class aye.class.
I have tried including the package name (com.java24hours) but it still doesn't work.. please help!
Commands I have tried:
java aye.class
java com.java24hours.aye.class
java aye
java com.java24hours.aye
program code:
package com.java24hours;
class aye {
public static void main(String[] args) {
//java code yeet
String aye = "Hello World!";
System.out.println(aye);
}
}
(I am running Linux on a Chromebook and have installed Java via the ppa:webupd8team/java)
Thanks.
I suppose you wanna put binaries to ./bin folder.
Compile aye.java:
javac -d ./bin aye.java
Then cd to ./bin directory and run the program:
cd bin
java com.java24hours.aye
well im stupid
since im new to java, i didn't know anything about packages and such. turns out all i had to do was put the class file in a folder named "ya" (that's the name of the package - i updated the program) and run the command
java -cp /home/ramsey/Documents/ya aye
(-cp stands for classpath, and you use it when you want to specify where you class is located MAKE SURE TO PUT IT IN A FOLDER NAMED AFTER YOUR PACKAGE!!!)
the wiki page is helpful: https://en.wikipedia.org/wiki/Classpath_(Java)
its under the section setting the path to execute java programs
thanks for the help everyone!

java able to compile but unable to find class error with cmd

Hello so recently I have started to transfer from c++ to java and one exercise is to compile and run a java program using cmd.
So okay, I coded my simple HelloWorld program using netbeans and saved it,
package helloworld;
public class Helloworld
{
public static void main(String[] args)
{
System.out.println("Hello world");
}
}
so now my saved .java file is in C:\Users\eatmybuns\Documents\NetBeansProjects\Helloworld\src\helloworld
now I open the cmd and I change the directory to the above and typed
javac Helloworld.java
and now I can see Helloworld.class in the same folder, I read from somewhere that I have to include the package name as well for it to run so I typed
java helloworld.Helloworld
it gave me an error so I tried running it from the src folder instead but it also gave me the same error.
Error: Could not find or load main class Helloworld
Caused by: java.lang.ClassNotFoundException: Helloworld
I have read some possible solutions such as using -cp or using -d but it keeps giving me the same error. I am currently using jdk1.8.0_161. on windows 10.
You have to use
java helloworld.Helloworld
and from the parentfolder of helloworld, which is the src directory, in your case.
There is a tight relationship between package and directory structure.
There are many flags you can set for the compiler, like srcdir, targetdir to keep classes and sources apart. But basically, when you invoke your class helloworld.Helloworld, the JVM looks for a directory helloworld/ and expects a Helloworld.class there.
To achive this without compiler flags, you have to put the source into the helloworld/ folder too.
The whole name of your class is helloworld.Helloworld and java should look there and find it there.
mkdir helloworld
mv Helloworld.java helloworld/
javac helloworld/Helloworld.java
java helloworld.Helloworld
Hello world
It's a bit surprising in the beginning, if you don't know it and started with classes without package declaration. But the logic is simple and straight forward: Every package is reflected by the directory structure.
With a distinction of sourcedir and targetdir, the directory structure below has to be the same as without, just the starting point differs. Common target dirs are classes or bin, like in:
javac -s . helloworld/Helloworld.java -d classes
or
javac -s ./src helloworld/Helloworld.java -d bin
But bin or classes don't get part of the package name, and you can't extend the invocation of the class by prepending that dir to the invocation path:
java bin.helloworld.Helloworld
won't work. But
java -cp ./bin helloworld.Helloworld
should. If you read the documentation carefully, you will find, that it carefully distinguishes source file (Helloworld.java), class (Helloworld) and file (Helloworld.class).

How to access another class in another package?

I have 2 classes, each is on different folders
Main class src/mainclass/Main.java
Display class src/swingclass/Display.java
Main Class:
package src.mainclass;
import src.swingclass.Display;
public class Main{
public static void main(String[] args){
Display sM = new Display();
}
}
Display Class:
package src.swingclass;
public class Display{
public Display(){
System.out.println("Display class");
}
}
i run it on cmd and it is stated
error: package src.swingclass does not exist
and also, Main class could not see Display class.
overall, i have 3 errors.
I know that using IDE will make this easier, but i want to learn this on cmd. I followed the tutorials on net, but it still has errors.
I believe you should get rid of the src part of the package, i.e.
package swingclass;
...
and import it like:
import swingclass.Display;
Remove all occurrences of "src." in all files
Change to the src directory
Type: javac mainclass/Main. java
Type: javac swingclass/Display.java
Type: java mainclass.Main
Go to the src folder level (go inside src folder) in your command prompt, and use the commands
javac -classpath . src/mainclass/Main.java
to compile (this will implicitly compile the Display class as well, since it is required by Main class) and
java -classpath . src/mainclass/Main
to run the Main class.
. indicates the current directory (in your case, src folder) relative to which the other paths (to source/class files) are then specified.
javac -d bin -sourcepath src <path to main class from src directory, in your case it will be one folder up from src/mainclass/Main.java>
java -cp bin src.mainclass.Main (set class path to bin root where class files are, scr is where your package starts)
Using the -classpath flag should help you solve your problem.
javac -d "theClassPathNameYouWant" "src/swingclass/Display.java"
javac -d "theClassPathNameYouWant" "src/swingclass/Main.java"
java -classpath "theClassPathNameYouWant" swingclass.Main

Java classpath and package problems - referencing multiple jar files

I have a java class I'm trying to run that references various other jar files. It looks something like this:
package com.myapp.test;
import java.io.File;
import com.myapp.ref;
import com.myapp.stuff;
import com.strangersapp.stuff;
import com.strangersapp.morestuff;
public class myTest {
public static void main...
...
}
The com.myapp.* classes are in another jar file called myapp.jar. The strangersapp classes are in a strangersapp.jar. There are several other referenced classes in various jars. My whole directory structure looks like this:
myapp.jar
strangersapp.jar
someother.jar
yetanother.jar
etc.jar
com\myapp\test\myTest.java
My problem is trying to compile and run this. I try the obvious but this command does NOT work. It doesnt pickup all the jar files:
javac -cp . com\myapp\test\myTest.java
java -cp . com.myapp.test.myTest
This command also does not work:
javac -cp ".;*.jar" com\myapp\test\myTest.java
However, these commands do work:
javac -cp ".;myapp.jar;strangersapp.jar;someother.jar;yetanother.jar;etc.jar" com\myapp\test\mTest.java
java -cp ".;myapp.jar;strangersapp.jar;someother.jar;yetanother.jar;etc.jar" com.myapp.test.mTest
Would anyone know why my first java run statement is not working?? I dont want to type all the jar names out in the classpath reference...
You should try this:
javac -cp ".;*.jar" com\myapp\test\myTest.java
java -cp ".;*.jar" com.myapp.test.myTest
If you use -cp you have to name all the jars you want to include. To avoid that just add them to your manifest and java will pick them up.
For javac i think you are supposed to use -classpath rather than -cp

I have managed to compile java-program but I cannot execute it

I have just installed JDK on Windows Vista. After that I set proper values for the 4 environment variables: classpath, include, lib, path. After that I was able to compile my HelloWorld-program (I got a *.class file). But when I try to execute the compiled program (I type java HelloWorldApp) it does not work. The Java write a lot of stuff and in the end it is written that it "could not find the main class: HelloWorldApp". Can anybody, pleas, help me with this problem?
Just for clarity; you are saying that you have a class in the default package, that is you have not included a package specifier in the Java file, and your class is called HelloWorldApp. When you compiled this, you got a classfile HelloWorldApp.class in the current directory.
Assuming the above to be true then try:
java -cp . HelloWorldApp
For example, the following works on a unix box:
$ echo 'class HelloWorldApp { public static void main(String []argv) { System.out.println("Hello World!"); } }' > HelloWorldApp.java
$ javac HelloWorldApp.java
$ java -cp . HelloWorldApp
Hello World!
Of course, you should indent your code a little nicer than just shoving the whole thing onto one line ;-)
Edit: To answer the comment:
Normally, the default classpath is the runtime libraries and the current directory. However, if you have the CLASSPATH variable set, then this will override the default, and you need to explicitly set the classpath back to its "default value". To verify if the CLASSPATH environment variable is set, you can do (again assuming unix):
set | grep CLASSPATH
If it is set, that is why you need to manually include . on your classpath.
create a file called HelloWorld.java;
paste the code posted below inside HelloWorld.java:
compile it by executing the command: javac HelloWorld.java in the same folder as HelloWorld.java is in;
execute the code by doing: java -cp . HelloWorld in the same folder as HelloWorld.java is in.
public class HelloWorld {
public static void main(String[] args) {
System.out.println("HelloWorld works!");
}
}
How the classpath works, can be read here: http://en.wikipedia.org/wiki/Classpath_%28Java%29
Have you included . and .. in your path? Just for clarification . represents your current directory and .. represents your parent directory. You are telling that the java has to search the current directory and the parent directory to find the class. Add the same to your classpath too.
What happens if you use:
java -cp {path to directory with HelloWorldApp in it} HelloWorldApp
That path should be contained within your CLASSPATH environment variable. Is that exported to your command shell ? Do you need to start a new command shell to get the most recent version of CLASSPATH ?
Post your code. I believe the problem is that your main class is not defined properly. I did this the other day.
public static void main(String[] args){
//code
}
The class path concept and the logical difference between Java source code and compiled byte code is notoriously hard to get right.
I would strongly recommend you familiarize yourself with the Sun Java Tutorial. The relevant section is
http://java.sun.com/docs/books/tutorial/getStarted/cupojava/win32.html

Categories