Debug Imported Files in VS Code - java

I have recently set up the Java extension for VS Code.I have a folder that contains two files Main.java and Person.java. Main.java calls Person.java. When I set a breakpoint in Main.java, everything works as normal.However, when I set a breakpoint in Person.java, it just skips over it.
Are there any workarounds in this issue?
Main.java file
public class Main {
public static void main(String[] args) {
System.out.println("Hello World!");
Person person = new Person();
person.speak();
}
}
Person.java file
public class Person {
public void speak(){
System.out.println("Speak!");
//breakpoint on this line right here gets skipped
System.out.println("Speak!");
}
}

Run -> Start Debugging, it stops at the breakpoint:

Related

Using package while running; Error "Could not find or load main class"

classpath= C:\Program Files\Java\jdk1.8.0_111\jre\lib\;C:\Program Files\Java\jdk1.8.0_111\bin;
path=C:\Program Files\Java\jdk1.8.0_111\bin;C:\Program Files\Java\jre1.8.0_111\bin;
package seleniumTest;
public class SampleTest {
public static void main(String[] args)
{
System.out.println("hello");
}
}
My question is I am not able run the Java file while using the package.
I am new to Java so not able to find the root cause. Please help me out.
Its not able to reach the main class.

how to solve package does not exist error in java

I am trying to compile my java file name Test.java. Test.java calling a class com.api.APIUser.java which is available in a user.jar file. I have added user.jar in lib folder. But Test.java is unable to pick APIUser.java. While I am compiling Test.java using javac I am getting error
"package com.api does not exist".
Test.java
import com.api.APIUser;
public class Test{
APIUser ap = new APIUser();
ap .login();
public static void main(String[] args){
//to do
}
}
APIUser
package com.api
public class APIUser{
public string login(){
//to do
return string;
}
}
If any one have idea why I am getting this error.please suggest me solution.
Thanks in advance.
put a semicolon after the package com.api like as below
package com.api;
clean and build the project and run if any issue inform
You have multiple issues in your code.
You have no line termination present for the com.api import in the APIUser class;
You have a syntax error in your login method.
Below is the improved code:
import com.api.APIUser;
public class Test {
// APIUser ap = new APIUser(); // This call should be in the method body,
// there is no use to keep it at the class level
// ap.login(); // This call should be in method body
public static void main(String[] args) {
// TO DO
APIUser ap = new APIUser();
ap.login();
}
}
APIUser
package com.api; // added termination here
public class APIUser {
//access specifier should be public
public string login(){
//to do
//return string;//return some value from here, since string is not present this will lead to error
return "string";
}
}
Also be sure that the JAR file is present in the classpath. If you are not using any IDE, you must use the -cp switch along with the JAR file path, so that a class can be loaded from there.
You can use the code below to understand how to compile your class using classpath from command prompt.
javac -cp .;/lib/user.jar; -D com.api.Test.java

setting java class path through php shell_exec in MAC

I am trying to run a java file from php through shell_exec . It works for simple jar files i.e. jar files with single class. Out of the below 2 commands the first one works fine. The second one consists of a package with two class , so to call particular class from it , I followed this calling procedure. Both the commands works fine in terminal. But the second command fails in shell_exec.
<?php
echo shell_exec("java -jar First.jar hi php");
echo shell_exec("java -cp samlePackage.jar:. samplePackage.Test");
?>
Here is First.class
class First
{
public static void main(String args[])
{
System.out.println(args[0]);
System.out.println(args[1]);
System.out.println("hello");
}
}
And here are the samplePackage classes
package samplePackage;
public class Hello
{
public void sayHello()
{
System.out.println("Hello");
}
}
package samplePackage;
public class Test
{
public static void main(String args[])
{
Hello obj = new Hello();
obj.sayHello();
}
}
I am not able to figure out my mistake. Please help.
Thanks in advance.

How to call a paint class from another .jar file

I have made a .jar file in Eclipse to call a paint class. When I finish it gives me an error:
JAR export finished with warnings. See details for additional information.
Exported with compile warnings: Graphics/src/G1.java
Jar export finished with problems. See details for additional information.
Could not find main method from given launch configuration.
Here is my code to call the paint method:
public class G1Starter {
public void main(String[] args)
{
Graphics1 g1 = new Graphics1();
g1.repaint();
}
}
I tried making a main method in the Graphics1 class but it did not work.
Add the static keyword so that the application has a valid entry point
public static void main(String[] args)
Method 'main' should be 'static'
public static void main(String[] args)

Recompiling the object during runtime

I am developing a project in java in which ,after running main file , some java files get altered and if i run that file again during the same execution the output does not show the changes done in the java file
For example there are 2 files. Main.java and file1.java
main.java
public static void main(string[] argv)
{
file1 obj = new file1();
obj.view();
Scanner in = new Scanner(System.in);
String x = in.nextLine();
//before entering any value i manually updated the content of file1.java
obj = new file1();
obj.view();
}
file1.java (Before updation)
public class file1
{
public void view()
{
system.out.println("This is test code!!");
}
}
file1.java (After updation)
public class file1
{
public void view()
{
system.out.println("That was done for Testing!!");
}
}
Output :
This is test code!!
This is test code!!
You have to recompile the code in order to see the changes.
What you can do is compile a string (after reading it from the file) with java and call methods of classes through reflection.
HERE is a step by step guide on how to compile a string programatically.
Updating a Java file will not impact the runtime instructions executed by the JVM.
When the compile a Java application the .java source code files are compiled into .class files containing byte code instructions which are in turn interpreted by the JVM. When the JVM requires a class it loads the appropriate .class file into memory via a special object known as a classloader.
Applying this to your example - when you first reference the class File1 the JVM will load the File1 class into memory. This in memory representation of the class will persist until either the classloader is destroyed or the JVM is restarted. No change to the file1.java class is visible to the JVM - firstly because the classloader wont reload the definition and secondly because the definition wont change until the file1.java class is recompiled.
To change an object's behaviour at runtime you can use the reflection API see here.
You don't need to compile source code to accomplish anything close to your example.
public class MyView
{
String the_string;
public MyView (String string) { the_string = string; }
public void setString (String string) { the_string = string; }
public void view () { system.out.println (the_string); }
}
public static void main(string[] argv)
{
MyView obj = new MyView("This is test code!!");
obj.view();
Scanner in = new Scanner(System.in);
obj.setString (in.nextLine());
obj.view();
}

Categories