import java.util.Scanner;
public class Twk {
public static void main(String[] Twk) {
System.out.println("Welcome to Twk");
Scanner enterName = new Scanner(System.in);
String name = enterName.next();
}
}
Why does it happen?
Twk.java and Twk.class are at the same place.
Need some helps, thanks.
Error: Could not find or load main class Twk
https://i.stack.imgur.com/NlcUu.png
Try with java -cp . Twk, this will probably let the java command "sees" the Twk.class file
You have to make sure public class exists in the source file.
Twk java and Twk class is the same program.
Twk java is the source file and Twk class is executable file of the source code generated by jdk compiler.
Do this if not yet:
public class Twk{
// your codes
}
This work:
C:\Users\tsoiwingkui>cd desktop
C:\Users\tsoiwingkui\Desktop>javac Twk.java
C:\Users\tsoiwingkui\Desktop>java Twk
Related
I'm on windows 10 + jdk1.8
Use used maven to create a project named UseNative, package name is mygroup, files are under:
src\main\java\mygroup\
I have UseNative.java:
package mygroup;
public class UseNative {
public native void greet(String name);
static {
System.loadLibrary("UseNative");
}
#SuppressWarnings("static-access")
public static void main(String[] args) {
System.out.println(System.getProperty("java.library.path"));
new UseNative().greet("me");
}
}
Then under powershell:
javac UseNative.java
OK, and then:
javah -classpath . UseNative
or
javah -classpath . mygroup.UseNative
Both says:
Error: cannot find class file for 'mygroup.UseNative'
I tried to remove the package mygroup line from java file, and then it works! But anyhow, I need this line to comply coding standard.
Where does it get wrong, add additional parameter or environment?
Thanks!
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.
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
Before people start flagging this question as a duplicate, know that I took the time too look at similar questions, and found that the answers to other "Error: Main method not found in class..." were not clearly applicable to my situation (according to my limited understanding of java)
I'm trying to utilize a text to speech api. Eclipse isn't complaining about the following code until I try to compile:
package com.textToSpeech;
import com.sun.speech.freetts.Voice;
import com.sun.speech.freetts.VoiceManager;
public class FreeTTS {
private static final String VOICENAME_kevin = "kevin";
private String text; // string to speech
public FreeTTS(String text) {
this.text = text;
}
public void speak() {
Voice voice;
VoiceManager voiceManager = VoiceManager.getInstance();
voice = voiceManager.getVoice(VOICENAME_kevin);
voice.allocate();
voice.speak(text);
}
public static void main(String[] args) {
String text = "FreeTTS was written by the Sun Microsystems Laboratories "
+ "Speech Team and is based on CMU's Flite engine.";
FreeTTS freeTTS = new FreeTTS(text);
freeTTS.speak();
}
}
The following error shows up in the console:
Error: Main method not found in class com.textToSpeech.FreeTTS, please define the main method as:
public static void main(String[] args)
The code above obviously has a main method, so does anyone know why I am getting this error, and furthermore how I can fix it?
I think it has something to do with the name of the class. If I change the name of the class to something like t2s and then try to compile, I get this error:
Error: Could not find or load main class com.textToSpeech.t2s
Anybody have any thoughts? Any help would be really appreciated.
You may have messed up your project properties. I don't use eclipse, so I cannot say for sure, but try creating a new project and adding the same code to it without fiddling with the properties. The class name and the file name should be the same, check that. Also make sure that the source file is in the same package folder. If nothing works, just create a new project.
Cheers.
I have written a grammar that allows the user to input a relative path. (e.g. "../../temp/out/path"
May aim is to get the absolute path based on the input from the user, and the absolute path of the current working directory so that I can also check if the input path is valid or not.
Is there libraries or built in functions that I can use to get the absolute path?
Something similar to C's _getcwd() function.
Yes, Java has a File class. You can create one by calling this constructor which takes a String. Then you can call getAbsolutePath() on it. You can call it like this:
package com.sandbox;
import java.io.File;
public class Sandbox {
public static void main(String[] args) {
File file = new File("relative path");
String absolutePathString = file.getAbsolutePath();
}
}
This will print a complete absolute path from where your application has initialized.
public class JavaApplication1 {
public static void main(String[] args) {
System.out.println("Working Directory = " +System.getProperty("user.dir"));
}
}