• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 324
  • Last Modified:

Problems with java 1.5 code in a java 1.4 enviroment

Dear Experts

I just discovered that my code doesn't in the UNIX Server because it has only support 1.4 versions

About the code:
The java code collects files from the Server that starts with gencube_ and ends with .log and puts them into a file called OUT.log.
The java code has to be sound in the error department meaning it should send an error if the path or the file type doesn't exit

And the most important part is the for loop that saves EVERY corresponding file to the Out.log

This is the code:



public class LogFileCopyPaste {
public static boolean fileExist = false;

public static void main(String[] args) throws SQLException, IOException {



File path = new File("app/home/ninja/java_test");
FileWriter fileWriter  = new FileWriter("app/home/ninja/java_test/OUT.log");
PrintWriter outputStream  = new PrintWriter(fileWriter);

if (!path.isDirectory()){
outputStream.println("Error 0001: The path doesn't exist");
fileExist = true;
}
else{
for (File mainFile :
path.listFiles(new FileFilter(){
public boolean accept(File f) {

String name = f.getName().toLowerCase();

//file name filtering
if(name.endsWith(".log")&&name.startsWith("gencube_",0)){
      fileExist = true;
      return true;
}
else{
      return false;
}


}
})//ends the path.listfiles
)//ends the for(File)
{

String dirLoc = mainFile.toString();
String []dirLoc2 = dirLoc.split("/n");
for(int v= 0; v<dirLoc2.length;v++){
System.out.println(dirLoc2[v]);      
outputStream.println(dirLoc2[v]);

}      

}
outputStream.close();
}
if (fileExist==false) {
outputStream.println("Error 0002: There are no Gencube");
}            
}
}
0
matrixrasta
Asked:
matrixrasta
  • 28
  • 21
  • 4
  • +1
1 Solution
 
ksivananthCommented:
all you have to update the for loop to be compatible with 1.4!
0
 
objectsCommented:
looks like you just need to compile it for 1.4
javac -target 1.4 -source 1.4 ....
0
 
matrixrastaAuthor Commented:
How is this done?
updating the for loop that is?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
ksivananthCommented:
>>for (File mainFile :
path.listFiles(new FileFilter(){
public boolean accept(File f) {

String name = f.getName().toLowerCase();

//file name filtering
if(name.endsWith(".log")&&name.startsWith("gencube_",0)){
      fileExist = true;
      return true;
}
else{
      return false;
}


}
})//ends the path.listfiles
)//ends the for(File)
>>

replace that with below,

File[] files = for (File mainFile :
path.listFiles(new FileFilter(){
public boolean accept(File f) {

String name = f.getName().toLowerCase();

//file name filtering
if(name.endsWith(".log")&&name.startsWith("gencube_",0)){
      fileExist = true;
      return true;
}
else{
      return false;
}


}
})//ends the path.listfiles
) ; //ends the for(File)

for( int i = 0; i < file.length; i++ ){
//String dirLoc = mainFile.toString();
String dirLoc = file[i].toString();
...
...
...
}
0
 
matrixrastaAuthor Commented:
There was a syntax error at the
>>File[] files = for (File mainFile :

Syntax error on token "=", ; expected
0
 
ksivananthCommented:
can you post your modified code?
0
 
matrixrastaAuthor Commented:
Now this is the modified code

public static boolean fileExist = false;
       
public static void main(String[] args) throws SQLException, IOException {
           
File path = new File("app/home/ninjat389461/java_test");
FileWriter fileWriter  = new FileWriter("app/home/t389461/java_test/OUT.log");
PrintWriter outputStream  = new PrintWriter(fileWriter);

if (!path.isDirectory()){
outputStream.println("Error 0001: The path doesn't exist");
fileExist = true;
}
else{
File[] files = for (File mainFile :
    path.listFiles(new FileFilter(){
    public boolean accept(File f) {

    String name = f.getName().toLowerCase();

    //file name filtering
    if(name.endsWith(".log")&&name.startsWith("gencube_",0)){
          fileExist = true;
          return true;
    }
    else{
          return false;
    }


    }
    })//ends the path.listfiles
    ) ; //ends the for(File)

{
                   
String dirLoc = mainFile.toString();
String []dirLoc2 = dirLoc.split("/n");
for(int v= 0; v<dirLoc2.length;v++){
    System.out.println(dirLoc2[v]);    
    outputStream.println(dirLoc2[v]);

}    
               
}
outputStream.close();
}
if (fileExist==false) {
outputStream.println("Error 0002: There are no Gencube Log Files in the directory");
}        
}
0
 
ksivananthCommented:
try this,

if (!path.isDirectory()){
outputStream.println("Error 0001: The path doesn't exist");
fileExist = true;
}
else{
File[] files = path.listFiles(new FileFilter(){
public boolean accept(File f) {

String name = f.getName().toLowerCase();

//file name filtering
if(name.endsWith(".log")&&name.startsWith("gencube_",0)){
      fileExist = true;
      return true;
}
else{
      return false;
}


}
})//ends the path.listfiles
) ; //ends the for(File)

for( int i = 0; i < files.length; i++ ){
String dirLoc = files[i].toString();
String []dirLoc2 = dirLoc.split("/n");
for(int v= 0; v<dirLoc2.length;v++){
    System.out.println(dirLoc2[v]);    
    outputStream.println(dirLoc2[v]);

}    
               
}
outputStream.close();
}
if (fileExist==false) {
outputStream.println("Error 0002: There are no Gencube Log Files in the directory");
}        
}
0
 
TimYatesCommented:
Like this (maybe)
  public static void main(String[] args) throws SQLException, IOException {
    File path = new File("app/home/ninjat389461/java_test");
    PrintWriter outputStream = new PrintWriter( new FileWriter( "app/home/t389461/java_test/OUT.log" ) ) ;
 
    if( !path.isDirectory() ) {
      outputStream.println("Error 0001: The path doesn't exist");
      fileExist = true;
    }
    else {
      File[] files = path.listFiles( new FileFilter() {
        public boolean accept(File f) {
          String name = f.getName().toLowerCase();
          //file name filtering
          if (name.endsWith(".log") && name.startsWith("gencube_", 0)) {
            fileExist = true;
            return true;
          } else {
            return false;
          }
        }
      } ) ; 
      for( int i = 0 ; i < files.length ; i++ )        
      {
        String dirLoc = files[ i ].toString();
        String[] dirLoc2 = dirLoc.split("/n");
        for (int v = 0; v < dirLoc2.length; v++) {
          System.out.println(dirLoc2[v]);
          outputStream.println(dirLoc2[v]);
        }
 
      }
      outputStream.close();
    }
    if (fileExist == false) {
      outputStream.println("Error 0002: There are no Gencube Log Files in the directory");
    }
  }

Open in new window

0
 
TimYatesCommented:
(PS:  I am just building on the work of others...this shouldn't be considered an answer on its own)
0
 
TimYatesCommented:
Lol...  Also, I was soundly beaten to the punch ;-)

Doh!  Too slow!
0
 
matrixrastaAuthor Commented:
thanks but there is a new error message:

Exception in thread "main" java.lang.UnsupportedClassVersionError: Test (Unsupported major.minor version 49.0)
        at java.lang.ClassLoader.defineClass0(Native Method)
        at java.lang.ClassLoader.defineClass(Unknown Source)
        at java.security.SecureClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.access$100(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClassInternal(Unknown Source)
0
 
TimYatesCommented:
Are you using the -target and -source parameters for javac like objects told you to in the second comment on this question?
0
 
ksivananthCommented:
looks like a class compiled in higher version is used! btw, what version of JDK you are using?
0
 
matrixrastaAuthor Commented:
sorry about that i forgot to configure the compiler in eclipse, i've just done that and now here is the next error:
Exception in thread "main" java.lang.NoSuchMethodError: java.io.PrintWriter.<init>(Ljava/lang/String;)V
        at Test.main(Test.java:18)
0
 
ksivananthCommented:
are you using any of the below ctors from PrintWriter,

PrintWriter(String fileName)
PrintWriter(File file)

these are available only from jdk1.5
0
 
matrixrastaAuthor Commented:
Yep im using the
>>PrintWriter outputStream = new PrintWriter( new FileWriter(
0
 
ksivananthCommented:
No, looks like, you use PrintWriter(String fileName)! change that to pass FileWriter as you posted in the last comment!
0
 
matrixrastaAuthor Commented:
This is the present code:

public static void main(String[] args) throws SQLException, IOException {
    File path = new File("app/home/ninjat389461/java_test");
    PrintWriter outputStream = new PrintWriter( new FileWriter( "app/home/t389461/java_test/OUT.log" ) ) ;
 
    if( !path.isDirectory() ) {
      outputStream.println("Error 0001: The path doesn't exist");
      fileExist = true;
    }
    else {
      File[] files = path.listFiles( new FileFilter() {
        public boolean accept(File f) {
          String name = f.getName().toLowerCase();
          //file name filtering
          if (name.endsWith(".log") && name.startsWith("gencube_", 0)) {
            fileExist = true;
            return true;
          } else {
            return false;
          }
        }
      } ) ;
      for( int i = 0 ; i < files.length ; i++ )        
      {
        String dirLoc = files[ i ].toString();
        String[] dirLoc2 = dirLoc.split("/n");
        for (int v = 0; v < dirLoc2.length; v++) {
          System.out.println(dirLoc2[v]);
          outputStream.println(dirLoc2[v]);
        }
 
      }
      outputStream.close();
    }
    if (fileExist == false) {
      outputStream.println("Error 0002: There are no Gencube Log Files in the directory");
    }
  }

and this one didn't work:

public static boolean fileExist = false;
       
public static void main(String[] args) throws SQLException, IOException {
           
File path = new File("app/home/ninjat389461/java_test");
FileWriter fileWriter  = new FileWriter("app/home/t389461/java_test/OUT.log");
PrintWriter outputStream  = new PrintWriter(fileWriter);

if (!path.isDirectory()){
outputStream.println("Error 0001: The path doesn't exist");
fileExist = true;
}
else{
File[] files = for (File mainFile :
    path.listFiles(new FileFilter(){
    public boolean accept(File f) {

    String name = f.getName().toLowerCase();

    //file name filtering
    if(name.endsWith(".log")&&name.startsWith("gencube_",0)){
          fileExist = true;
          return true;
    }
    else{
          return false;
    }


    }
    })//ends the path.listfiles
    ) ; //ends the for(File)

{
                   
String dirLoc = mainFile.toString();
String []dirLoc2 = dirLoc.split("/n");
for(int v= 0; v<dirLoc2.length;v++){
    System.out.println(dirLoc2[v]);    
    outputStream.println(dirLoc2[v]);

}    
               
}
outputStream.close();
}
if (fileExist==false) {
outputStream.println("Error 0002: There are no Gencube Log Files in the directory");
}        
}


0
 
ksivananthCommented:
can u post the stack now?
0
 
matrixrastaAuthor Commented:
That's the stack above! Or what do you mean by stack to be precise
This is what you suggested I should do


public static boolean fileExist = false;
       
public static void main(String[] args) throws SQLException, IOException {
           
File path = new File("app/home/ninjat389461/java_test");
FileWriter fileWriter  = new FileWriter("app/home/t389461/java_test/OUT.log");
PrintWriter outputStream  = new PrintWriter(fileWriter);
if (!path.isDirectory()){
outputStream.println("Error 0001: The path doesn't exist");
fileExist = true;}
else{
File[] files = for (File mainFile :
    path.listFiles(new FileFilter(){
    public boolean accept(File f) {
    String name = f.getName().toLowerCase();
    //file name filtering
    if(name.endsWith(".log")&&name.startsWith("gencube_",0)){
          fileExist = true;
          return true;
    }
    else{
          return false;
    }
    }
    })//ends the path.listfiles
    ) ; //ends the for(File)
{                    
String dirLoc = mainFile.toString();
String []dirLoc2 = dirLoc.split("/n");
for(int v= 0; v<dirLoc2.length;v++){
    System.out.println(dirLoc2[v]);    
   outputStream.println(dirLoc2[v]);
}                    
}
outputStream.close();
}
if (fileExist==false) {
outputStream.println("Error 0002: There are no Gencube Log Files in the directory");
}        
}


And this is the error message which was created

Syntax error on token "=", ; expected towards the >>File[] files = for (File mainFile :
0
 
ksivananthCommented:
you repeat the earlier cleared problem!

when you have the below code, what is the erro message you get, may the emssage is same but diff line number,

statck - exception stack trace!

public static void main(String[] args) throws SQLException, IOException {
    File path = new File("app/home/ninjat389461/java_test");
    PrintWriter outputStream = new PrintWriter( new FileWriter( "app/home/t389461/java_test/OUT.log" ) ) ;
 
    if( !path.isDirectory() ) {
      outputStream.println("Error 0001: The path doesn't exist");
      fileExist = true;
    }
    else {
      File[] files = path.listFiles( new FileFilter() {
        public boolean accept(File f) {
          String name = f.getName().toLowerCase();
          //file name filtering
          if (name.endsWith(".log") && name.startsWith("gencube_", 0)) {
            fileExist = true;
            return true;
          } else {
            return false;
          }
        }
      } ) ;
      for( int i = 0 ; i < files.length ; i++ )        
      {
        String dirLoc = files[ i ].toString();
        String[] dirLoc2 = dirLoc.split("/n");
        for (int v = 0; v < dirLoc2.length; v++) {
          System.out.println(dirLoc2[v]);
          outputStream.println(dirLoc2[v]);
        }
 
      }
      outputStream.close();
    }
    if (fileExist == false) {
      outputStream.println("Error 0002: There are no Gencube Log Files in the directory");
    }
  }


0
 
matrixrastaAuthor Commented:
Thanks
I entered your above code and is showed this error
Exception in thread "main" java.io.FileNotFoundException: app/home/t389461/java_test/OUT.log (No such file or directory)

And I noticed that I'm in the folder app/home/t389461/java_test/ so I removed changed the code to
PrintWriter outputStream = new PrintWriter( new FileWriter( "OUT.log" ) ) ;
and now it creates the OUT.log file but doesn't fill it with the gencube*.log names

0
 
ksivananthCommented:
>>File path = new File("app/home/ninjat389461/java_test");

modify the path appropriately!
0
 
ksivananthCommented:
//try this, if your in the same folder
File path = new File("");
0
 
matrixrastaAuthor Commented:
sorry about that (Type error) but thats was the path i used.
I would just like a basic code that can collect all the gencube files and System.out.println(the gencubenames)
0
 
ksivananthCommented:
can you print and post the result?
System.out.println( "Directory Name: " + new File( "" ).getAbsolutePath() ) ;
0
 
matrixrastaAuthor Commented:
Directory Name: /app/home/ninjat389461/java_test
0
 
ksivananthCommented:
>>System.out.println(dirLoc2[v]);

what that prints?
0
 
matrixrastaAuthor Commented:
When I type: ls
Result:
test.txt
gencube_2IN.log                                          Two.jar
gencube_IN.log                                           UnixRunner.jar
GenList.txt                                              unixrunner.ksh
IN.txt                                                   UnixTest.ksh
OUT.log                                                  UnixTest.log
OUT.txt                                                  UnixTest002.jar
0
 
matrixrastaAuthor Commented:
>>System.out.println(dirLoc2[v]);
It does nothing hence it doesn't enter the loop
0
 
matrixrastaAuthor Commented:
And When I type :
$ pwd
/app/home/ninjat389461/java_test
0
 
ksivananthCommented:
String dirLoc = files[ i ].toString();
System.out.println(dirLoc);
        String[] dirLoc2 = dirLoc.split("/n");

can you post what it prints? also dirLoc.split("/n"); wht are you trying to do with this?
0
 
matrixrastaAuthor Commented:
It does noting! Like i said it doesn't get to the loop and as for the dirLoc.split was the code that was written for me what it actually does i really don't know. I'll try to use the System.out to trace where the loop stops
0
 
ksivananthCommented:
can you print the name of the file after the line
name
//file name filtering
System.out.println("Listed File NAME: " + name );
0
 
matrixrastaAuthor Commented:
Okay the dirLoc.split("/n"); separates all the filenames and is kept in an array i guess
0
 
matrixrastaAuthor Commented:
nothing in the unix server but in the windows Listed File NAME: gencube_2in.log

P.S. In the windows i change the path name and it does work in windows
0
 
matrixrastaAuthor Commented:
I've cross checked the Unix path and every thing is up to scratch

$ cd /
$ cd app/home/ninjat389461/java_test
$ pwd
/ app/home/ninjat389461/java_test
0
 
ksivananthCommented:
>>nothing in the unix server but in the windows Listed File NAME: gencube_2in.log

why its not prinitng all file names? can u post the current code?
0
 
matrixrastaAuthor Commented:
     public static boolean fileExist = false;

      
      public static void main(String[] args) throws IOException {
          File path = new File("app/home/ninjat389461/java_test");
            
          PrintWriter outputStream = new PrintWriter( new FileWriter( "OUT.log" ) ) ;
          
          if( !path.isDirectory() ) {
            outputStream.println("Error 0001: The path doesn't exist");
            fileExist = true;
          }
          else {
            File[] files = path.listFiles( new FileFilter() {
              public boolean accept(File f) {
                String name = f.getName().toLowerCase();
                System.out.println("Listed File NAME: " + name );
                //file name filtering
                if (name.endsWith(".log") && name.startsWith("gencube_", 0)) {
                  fileExist = true;
                  return true;
                } else {
                  return false;
                }
              }
            } ) ;
            for( int i = 0 ; i < files.length ; i++ )        
            {
                
              String dirLoc = files[ i ].toString();
              String[] dirLoc2 = dirLoc.split("/n");
              for (int v = 0; v < dirLoc2.length; v++) {
                System.out.println(dirLoc2[v]);
                outputStream.println(dirLoc2[v]);
              }
              
            }
            outputStream.close();
            
          }
          if (fileExist == false) {
            outputStream.println("Error 0002: There are no Gencube Log Files in the directory");
          }
          //System.out.println( "Directory Name: " + new File("").getAbsolutePath() ) ;
        }
0
 
ksivananthCommented:
>>System.out.println("Listed File NAME: " + name );

this should print all the files under that directory but you are saying it prints just one file in windows and nothing in unix, strange!
0
 
ksivananthCommented:
are you sure if the files are not hidden?
0
 
matrixrastaAuthor Commented:
i dont this so:
$ ls -l gencube_*.log
-rw-r-----   1 ninjat389461  GEN01      11445 Apr  9 13:25 gencube_2IN.log
-rw-r-----   1 ninjat389461  GEN01      11445 Apr  9 13:25 gencube_IN.log
0
 
matrixrastaAuthor Commented:
false
Its in the Else loop
Exception in thread "main" java.lang.NullPointerException
        at Two.Two.main(Two.java:39)

That is the for( int i = 0 ; i < files.length ; i++ ){

package Two;
import java.io.*;

/**
 *
 */

/**
 * @author t389461
 *
 */
public class Two {
      public static boolean fileExist = false;
      
      public static void main(String[] args) throws IOException {
          File path = new File("app/home/ninjat389461/java_test");
            
          PrintWriter outputStream = new PrintWriter( new FileWriter( "OUT.log" ) ) ;
          System.out.println(path.isDirectory());
          /*if( !path.isDirectory() ) {
            outputStream.println("Error 0001: The path doesn't exist");
            fileExist = true;
          }
          else {*/
                System.out.println("Its in the Else loop");
            File[] files = path.listFiles( new FileFilter() {
              public boolean accept(File f) {
                String name = f.getName().toLowerCase();
                System.out.println("Listed File NAME: " + name );
                //file name filtering
                if (name.endsWith(".log") && name.startsWith("gencube_", 0)) {
                  fileExist = true;
                  return true;
                } else {
                  return false;
                }
              }
            } ) ;
            for( int i = 0 ; i < files.length ; i++ ){
                String dirLoc = files[ i ].toString();
              String[] dirLoc2 = dirLoc.split("/n");
              for (int v = 0; v < dirLoc2.length; v++) {
                System.out.println(dirLoc2[v]);
                outputStream.println(dirLoc2[v]);
              }
              
            }
            outputStream.close();
            
          //}
          if (fileExist == false) {
            outputStream.println("Error 0002: There are no Gencube Log Files in the directory");
          }
          //System.out.println( "Directory Name: " + new File("").getAbsolutePath() ) ;
        }


      
}
0
 
matrixrastaAuthor Commented:
Or better still if you have made a code similar to that that working unix can you post it please?
0
 
matrixrastaAuthor Commented:
In Windows the System.out.println(path.isDirectory()); is true in Windows and False in Unix
0
 
ksivananthCommented:
System.out.println(path.getAbsolutePath());
System.out.println(path.exists()) ;

what it prints in Unix?
0
 
matrixrastaAuthor Commented:
like i said b4  /app/home/ninjat389461/java_test.
Do you have a working code that recieves specific files in a Unix enviroment?
0
 
ksivananthCommented:
>>Do you have a working code that recieves specific files in a Unix enviroment?

yes I do but its not that different than you have!

can you post the result of below?

System.out.println(path.exists()) ;
System.out.println(path.canRead()) ;
System.out.println(path.isFile()) ;
System.out.println(path.isDirectory()) ;
System.out.println(path.isHidden()) ;
0
 
matrixrastaAuthor Commented:
I actually dont have the pc at my disposal at the moment. Ill send you the result first thing tomorrow
Thanks for your support
0
 
matrixrastaAuthor Commented:
Thanks for you help. I actually found an alternative and it works just fine for me.

Java Code:
public static void main(String[] args) throws IOException {

LogFilePlus lfp = new LogFilePlus();      
Runtime runtime = Runtime.getRuntime();

//Unix Command
Process process = runtime.exec(lfp.UnixExecute("ls /app/home/ninja/UnixParse"));

InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;

while ((line = br.readLine()) != null) {      
//line holds every file that starts and end with the followings
if(line.toLowerCase().endsWith(".log")&& line.toLowerCase().startsWith("gencube_",0)){


Runtime runtime2 = Runtime.getRuntime();
Process process2 = runtime2.exec(lfp.UnixExecute("/app/home/ninja/get_transformer_pid.sh /app/gen/dyn/gen/logs/"+line));

InputStream is2 = process2.getInputStream();
InputStreamReader isr2 = new InputStreamReader(is2);
BufferedReader br2 = new BufferedReader(isr2);
String line2;

while ((line2 = br2.readLine()) != null) {      

if(line2.equals("")){
      System.out.println("The Log-File "+line +" is closed");                                    
}
else{
      System.out.println("It is opened");
}
}                        
}
}
}


lfp.UnixExecute(String sCommand) is

public String UnixExecute(String sCommand){
 String stemp = sCommand;
 return stemp;
}


0
 
matrixrastaAuthor Commented:
Thanks any way
0
 
ksivananthCommented:
this is nothing but introducing unecessay complication!

you should be able to traverse through file.list!
0
 
matrixrastaAuthor Commented:
>> this is nothing but introducing unecessay complication!
What exactly do you mean? Could you please post you suggestion(Java code that is!)
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

  • 28
  • 21
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now