In Java I need to run a local method (code below) - java

Hey I have been trying to get the main to run the methods but I don't remember how to do it.
It is a simple program so far because I just started on it 15 minutes ago.
`
import java.awt.Robot;
import javax.swing.JFrame;
import java.lang.*;
import java.io.*;
public class sweetRevenge {
public static void main(String[] args) {
//call local static classes browserjacker and wallpaperjacker
start(browserJacker(1));
}
public static void browserJacker(int i)throws IOException{
try
{
//include bad things along with self made video of hacking linked to youtube
Process p=Runtime.getRuntime().exec("cmd /c start http://www.google.com");
}
catch(IOException e1) {System.out.println(e1);}
}
public static void wallpaperJacker (String args []) throws IOException {
Process p=Runtime.getRuntime().exec("cmd /c start");
}}
`

since your method is throwing an exception so the place where you will call it MUST have an exception handling mechanism, and your code needs to be modified.
import java.awt.Robot;
import javax.swing.JFrame;
import java.lang.*;
import java.io.*;
public class sweetRevenge {
public static void main(String[] args) {
//call local static classes browserjacker and wallpaperjacker
try{
browserJacker(); // will start method browserJacker
// since the method is static so it can be easily accessed from static method
}catch{IOException ex}{
}
}
public static void browserJacker()throws IOException{
//include bad things along with self made video of hacking linked to youtube
Process p=Runtime.getRuntime().exec("cmd /c start http://www.google.com");
}
public static void wallpaperJacker () throws IOException {
Process p=Runtime.getRuntime().exec("cmd /c start");
}
}

Related

Java - How do I call a class's method with a string?

I'm trying to use my input from the console to pick which class's main method I want to run.
package run;
import java.lang.reflect.Method;
import java.util.Scanner;
import testing.*;
public class run {
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, SecurityException{
Scanner input = new Scanner(System.in);
String name = "testing."+input.nextLine();
Class Program = Class.forName(name);
//Try 1
Program obj = new Program();
//Got error "Program cannot be resolved to a type" on program and program
//Try 2
Program.main();
//Got error "The method main() is undefined for the type Class" on main
//Try 3
Class.forName(name).main();
//Got error "The method main() is undefined for the type Class<capture#2-of ?>" on main
}
}
Class program = Class.forName(name);
program.getDeclaredMethod("main", String[].class).invoke(null, new Object[]{args});
provide your main method is public static void main(String[] args)
This problem can be solved easily using Reflection. Check out the code below:
package run;
import java.lang.reflect.Method;
import java.util.Scanner;
import testing.*;
public class run {
public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, SecurityException, NegativeArraySizeException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
Scanner input = new Scanner(System.in);
String name = "testing."+input.nextLine();
Class program = Class.forName(name);
program.getMethod("main", String[].class).invoke(null, Array.newInstance(String.class, 0));
}
}

Freetts is not working with external speakers

I have used Freetts.jar file in my java application that announces the token number. My application is working perfectly in my laptop but is not working in my desktop that has an external speaker. I get a null pointer exception. NOTE: I use Windows 7 in both my computers.
The Below Code is the Sample Format I used.
package tsapp;
import java.util.Locale;
import javax.speech.Central;
import javax.speech.synthesis.Synthesizer;
import javax.speech.synthesis.SynthesizerModeDesc;
import javax.swing.JOptionPane;
public class TextSpeech {
public static void main(String[] args){
try
{
System.setProperty("freetts.voices",
"com.sun.speech.freetts.en.us.cmu_us_kal.KevinVoiceDirectory");
Central.registerEngineCentral
("com.sun.speech.freetts.jsapi.FreeTTSEngineCentral");
Synthesizer synthesizer =
Central.createSynthesizer(new SynthesizerModeDesc(Locale.US));
synthesizer.allocate();
synthesizer.resume();
String str;
str=JOptionPane.showInputDialog(null,"Voice Check");
if(str==null)
return;
synthesizer.speakPlainText(str, null);
synthesizer.waitEngineState(Synthesizer.QUEUE_EMPTY);
synthesizer.deallocate();
}
catch(Exception e)
{
System.out.println(e.getClass());
e.printStackTrace();
}
}
}
Can we do one simple thing:
Download binary https://netix.dl.sourceforge.net/project/freetts/FreeTTS/FreeTTS%201.2.2/freetts-1.2.2-bin.zip
Add to your project
Write simple code.
Like this
import com.sun.speech.freetts.Voice;
import com.sun.speech.freetts.VoiceManager;
public class TestVoice {
public static void main(String[] args) {
String text = "Voice check!";
Voice voice;
VoiceManager voiceManager = VoiceManager.getInstance();
voice = voiceManager.getVoice("kevin");
voice.allocate();
voice.speak(text);
}
}
Just be sure that all these libs are on your desktop too.

"Attributes and objects cannot be resolved" - error

The following code is for reading or writing files with java, but:
Eclipse prints these errors:
buffer_1 cannot be resolved to a variable
file_reader cannot be resolved
also other attributes...
what is wrong in this code here:
//Class File_RW
package R_2;
import java.io.File;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.lang.NullPointerException;
public class File_RW {
public File_RW() throws FileNotFoundException, NullPointerException {
File file_to_read = new File("C:/myfiletoread.txt");
FileReader file_reader = new FileReader(file_to_read);
int nr_letters = (int)file_to_read.length()/Character.BYTES;
char buffer_1[] = new char[nr_letters];
}
public void read() {
file_reader.read(buffer_1, 0, nr_letters);
}
public void print() {
System.out.println(buffer_1);
}
public void close() {
file_reader.close();
}
public File get_file_to_read() {
return file_to_read;
}
public int get_nr_letters() {
return nr_letters;
}
public char[] get_buffer_1() {
return buffer_1;
}
//...
}
//main method # class Start:
package R_2;
import java.io.File;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.lang.NullPointerException;
public class Start {
public static void main(String[] args) {
File_RW file = null;
try {
file = new File_RW();
} catch (NullPointerException e_1) {
System.out.println("File not found.");
}
//...
}
}
I can't find any mistake. I have also tried to include a try catch statement into the constructor of the class "File_RW", but the error messages were the same.
Yes, there are errors in your code - which are of really basic nature: you are declaring variables instead of fields.
Meaning: you have them in the constructor, but they need to go one layer up! When you declare an entity within a constructor or method, then it is a variable that only exists within that constructor/method.
If you want that multiple methods can make use of that entity, it needs to be a field, declared in the scope of the enclosing class, like:
class FileRW {
private File fileToRead = new File...
...
and then you can use your fields within all your methods! Please note: you can do the actual setup within your constructor:
class FileRW {
private File fileToRead;
public FileRW() {
fileToRead = ..
but you don't have to.
Finally: please read about java language conventions. You avoid using "_" within names (just for SOME_CONSTANT)!
javacode already running...thx
same program edited with c++ in visual Studio express...
visit the stackoverflow entry link:
c++ file read write-error: Microsoft Visual C++ Runtime libr..debug Assertion failed, expr. stream.valid()

End of Audio file in java

I have written this code for playing audio file, I want to get indication when my audio file ends after playing. I have tried AS.getMicrosecondLength() == AS.getMicrosecondPosition() but these methods are undefined for the AudioStream. Please tell how I can do that.
import java.io.FileInputStream;
import sun.audio.AudioPlayer;
import sun.audio.AudioStream;
public class A {
public static void main(String arg[]) throws Exception {
AudioStream AS = new AudioStream(new FileInputStream("sounds.wav"));
AudioPlayer.player.start(AS);
}
}
AS.getMicrosecondLength() == AS.getMicrosecondPosition() could be used to set off the flag when the clip has ended

Reading Audio file in java

I have written this code for playing audio file,and I want to get indication when my audio file ends after playing. I have tried AS.getMicrosecondLength() == AS.getMicrosecondPosition() but these methods are undefined for the AudioStream.
My Code:
import java.io.FileInputStream;
import sun.audio.AudioPlayer;
import sun.audio.AudioStream;
public class A {
public static void main(String arg[]) throws Exception {
AudioStream AS = new AudioStream(new FileInputStream("sounds.wav"));
AudioPlayer.player.start(AS);
}
}
Please tell how I can solve my problem

Categories

Resources